Last Words of Prophet Muhammad
Question

What were the last words of the Prophet (peace and blessings of Allah be upon him) before he bade farewell to this world?
Praise be to Allah.The last words of the Prophet (peace and blessings of Allah be upon him) – before he bade farewell to this world – were: “O Allah, (with) the higher companions”. 
This is the title that Al-Bukhari (may Allah have mercy on him) gave to a chapter in Kitab Al-Maghazi in his Sahih: “The last words of the Prophet (peace and blessings of Allah be upon him)”, where he quoted the Hadith of `Aishah (may Allah be pleased with her) who said: 
When the Prophet was healthy, he used to say, “No soul of a Prophet is taken until he has been shown his place in Paradise and then he is given the choice.” When death approached him while his head was on my thigh, he became unconscious and then recovered consciousness. He then looked at the ceiling of the house and said, “O Allah! (with) the highest companions." I said (to myself), “He is not going to choose (to stay with) us.” Then I understood what he meant when he said that to us when he was healthy. The last words he spoke were, "O Allah! (with) the highest companions.” (Narrated by Al-Bukhari, 4463; Muslim, 2444)
There is a report narrated by Ahmad (1691) from the Hadith of Abu `Ubaydah (may Allah be pleased with him), who said that the last words that the Prophet (peace and blessings of Allah be upon him) spoke were, “Expel the Jews of the Hijaz and Najran from the Arabian Peninsula, and know that the most evil of people are those who took the graves of their Prophets as places of worship.” This was classed as authentic by Al-Albani in As-Sahihah, 1132. 
And Abu Dawud (5156)and Ibn Majah (2698) narrated that `Ali (may Allah be pleased with him) said: The last words that the Messenger of Allah (peace and blessings of Allah be upon him) spoke were, “The prayer, the prayer! And fear Allah with regard to those whom your right hands possess.” This was classed as authentic by Al-Albani in Sahih Abu Dawud. And there are other similar Hadiths. 
What is meant by these reports is that these are some of the last things that the Prophet (peace and blessings of Allah be upon him) said, or they were the last pieces of advice that the Prophet (peace and blessings of Allah be upon him) gave to his family and companions, and those who would be in authority after he was gone. These Hadiths are among the last things he said, but the Hadith of `Aishah (may Allah be pleased with her) is the last of all. (See Fayd Al-Qadir by Al-Manawi, 5/250 – 251) 
As-Suhayli (may Allah have mercy on him) said: “The reason why these – “O Allah, (with) the higher companions” – were the last words of the Prophet (peace and blessings of Allah be upon him) is because they refer both to Tawhid and to Dhikr in the heart. It offers comfort to those who are unable to speak (when dying), because some people may not be able to speak out loud for some reason, but that does not matter if their hearts are steadfast in remembering Allah.” (Quoted by Al-Hafiz in Al-Fath, 8/138)
For more details, please refer to the following answers: 11575 , 113393 , 197199 , 222848 , and 258503 . 
And Allah knows best.