The very word that one has to utter to embrace Islam affirms two basic realities: first, the oneness of Allah as the Only God to be worshipped; second, the necessity of following the only way to Him: the path of His Messenger Muhammad (peace and blessings be upon him). This fact has always to be present in the mind of each Muslim when he intends to do a good deed.

Answering this question, Sheikh MuhammedSalih Al-Munajjid, a prominent Saudi Muslim lecturer and author, said:

For acts of worship to be acceptable by Allah and for a person to be rewarded for them, there are two conditions that must be fulfilled:
The first condition is that actions should be devoted to Allah Alone. Allah says, (And they were commanded not but that they should worship Allah, and worship none but Him Alone (abstaining from ascribing partners to Him))(Al-Bayyinah 98:5).
The meaning of worshipping Allah alone is that the person should intend in all his words and deeds, both inward and outward, to please Allah. Allah says, (And who has no favor from anyone to be paid back, except to seek the countenance of his Lord, the Most High.) (Al-Layl 92: 19)
(We feed you seeking Allah’s countenance only. We wish for no reward nor thanks from you.) (Al-Insan 76: 9)
(Whosoever desires (by his deeds) the reward of the hereafter, We give him increase in his reward, and whosoever desires the reward of this world (by his deeds), We give him thereof (what is decreed for him), and he has no portion in the hereafter.) (Ash-Shura 42: 20)

(Whosoever desires the life of the world and its glitter — to them We shall pay in full (the wages of) their deeds therein, and they will have no diminution therein. They are those for whom there is nothing in the Hereafter but Fire, and vain are the deeds they did therein. And of no effect is that which they used to do)(Hud 11:15–16).

It was narrated that ‘Umaribn al-Khattab (may Allah be pleased with him) said: “I heard the Messenger of Allah (peace and blessings be upon him) say,  “The reward of deeds depends upon the intentions and every person will get the reward according to what he has intended. So whoever emigrated for worldly benefits or for a woman to marry, his emigration was for what he emigrated for.” (Al-Bukhari)

Muslim reported on the authority of Abu Hurairah that the Messenger of Allah (peace and blessings be upon him) said, “Allah (exalted be He) says, ‘I am so Self-Sufficient that I am in no need of having an associate. Thus he who does an action for someone else’s sake as well as Mine will have that action renounced by Me to him whom he associated with Me‘.” (Muslim)

The second condition is that actions should be in accordance with the only way that Allah has prescribed for worship, which is following the Prophet (peace and blessings be upon him) in the laws that he brought. It was narrated that the Prophet (peace and blessings be upon him) said, “Whoever does any action that is not in accordance with this matter of ours [i.e., Islam] will have it rejected” (Muslim).

IbnRajab (may Allah have mercy on him) commented: This hadith constitutes one of the most important principles of Islam. It is like a scale for weighing up deeds according to their outward appearance, just as the hadith saying “The reward of deeds depends upon intentions” is the scale for weighing up the inner nature of deeds. Just as every action that is not intended for the sake of Allah brings no reward to the one who does it, every deed that is not in accordance with the command of Allah and the command of His Messenger will be rejected. For anyone who innovates something in Islam concerning which Allah and His Messenger have not granted permission, that thing will not be considered part of Islam. (Jami’ al-‘Ulumwa’l-Hikam, part 1, p. 176)

The Prophet (peace and blessings be upon him) enjoined following his Sunnah and teachings, and made this binding. He (peace and blessings be upon him) said, “You have to follow my Sunnah (way) and the way of my Rightly Guided Successors; bite onto them with your eyeteeth (i.e., cling firmly to them).”
He also warned against bid`ahs (innovation in religion), as he said, “Beware of newly-invented matters, for every newly-invented matter is a going astray.” (Al-Tirmidhi, Al-Ilm)

Ibn Al-Qayyim said: Allah has made devotion in worshipping Him alone and following the Sunnah the means of accepting deeds; if these conditions are not fulfilled, then deeds will be unacceptable” (Ar-Ruh [The Soul], 1/135).
Allah says,(Who has created death and life that He may test you which of you is best in deed)(Al-Mulk 67: 2).
Al-Fudayl said: (best in deed)means the most devoted to Allah alone and the nearest to the Sunnah.