The relationship between actions and intentions:
Scholars have differed as to the exact meaning of the words: "Actions are but by intentions." Some later scholars have advanced the opinion that it refers to actions that are correct, valid, and accepted in Islam. This opinion restricts the meaning of this hadîth to specific acts of devotion that require an intention to be acceptable and worthy of reward, like prayer, fasting, and the obligatory emigration to Madinah.
As for other activities like eating and drinking - or even virtuous acts like fulfilling one's trusts - would not fall under the meaning of this hadîth if we were to accept this view.
Others are of the opinion that this hadîth is talking about actions in general. This is the most correct opinion, and it is the view of the majority of scholars, especially the earlier ones. This was clearly what Ahmad b. Hanbal understood from the hadîth, because he said: "I prefer that when anyone does any deed - whether it be prayer, fasting, or charity, or whether it be any other kind of virtuous deed - he should have his intentions always precede his actions, for the Prophet (peace be upon him) said: 'Actions are but by intentions…'."
According to this understanding, the hadîth is addressing all of our voluntary actions, since everything we do has some sort of intention behind it. When the Prophet said: "…and every man will have only what he intended", he was informing us of the implications of this in Islamic Law. If a person performs a good and correct deed with a good intention, then his action will be good and rewardable, and if he performs the same action with a bad motive, then his action will be bad and deserving of punishment.
Likewise, if a person performs a permissible action without an intention to do something good or bad, but merely with the intention of doing something permissible, then he will be deserving of neither a reward nor a punishment for doing so.
Deeds can be good and bad in and of themselves. However, a person will only be deserving of a reward for his good deeds if he performed them for the sake of Allah and not for some ulterior motive.
Allah says: "There is no good in much of their secret talk save (in) him who enjoins almsgiving and goodness and peace-making among the people. Whoever does this seeking the good pleasure of Allah, We shall bestow on him a vast reward." [Sûrah al-Nisâ': 114]
Ibn Rajab comments on this verse that Allah describes enjoining almsgiving, goodness, and reconciliation as good deeds in and of themselves. This is because such deeds benefit the people. However, Allah only promises a reward to those who carry out these deeds "seeking the good pleasure of Allah". Therefore, if a person performs these good and beneficial deeds for the sake of Allah, then the deeds will be good for him as well as for those who benefit from his deeds. If he had other ulterior motives, then these deeds will be of no good for him. [Jâmi` al-`Ulûm wa al-Hikam]
Ibn Rajab further observes that a person who prays, fasts, and recites Allah's name with some ulterior motive is performing deeds that have no good in them whatsoever. The person who performs such deed in such a manner is earning sin for doing so. At the same time, his deeds do not bring the least benefit to anybody else.
You can read more
here