Actions are judged by intentions

Every deed, prayer included, is judged by the intention behind it.

Narrated by Umar ibn al-Khattab

Actions are judged by their intentions, and every person will have what they intended. So whoever emigrated for Allah and His Messenger, his emigration is for Allah and His Messenger; and whoever emigrated for something of this world or to marry a woman, his emigration is for what he emigrated for.

Sahih al-Bukhari 1, Sahih Muslim 1907

What this hadith teaches

Scholars open their books with this hadith because it is the basis of every act of worship. A prayer offered sincerely for Allah is rewarded; the same movements done to be seen by people are not.

Reference and grade

Grade
Sahih: agreed upon by al-Bukhari and Muslim
Narrator
Umar ibn al-Khattab

The hadith in Arabic نص الحديث

إِنَّمَا الأَعْمَالُ بِالنِّيَّاتِ، وَإِنَّمَا لِكُلِّ امْرِئٍ مَا نَوَى، فَمَنْ كَانَتْ هِجْرَتُهُ إِلَى دُنْيَا يُصِيبُهَا أَوْ إِلَى امْرَأَةٍ يَنْكِحُهَا فَهِجْرَتُهُ إِلَى مَا هَاجَرَ إِلَيْهِ

Arabic text of Sahih al-Bukhari 1, as given on sunnah.com.