Ibn Abu Laila reported: Sahl ibn Hunaif and Qais ibn Sa’d ibn Ubaidah were in Al-Qadisiyyah when a funeral passed by them, so they stood up and it was said to them, “It is one of the local people.” They both said: A funeral passed by the Messenger of Allah, peace and blessings be upon him, and he stood up. It was said to him, “It is a Jew.” So the Prophet said, “Was he not a soul?”
Source: Sahih Bukhari 1250, Sahih Muslim 961
Grade: Muttafaqun Alayhi (authenticity agreed upon) according to Al-Bukhari and Imam Muslim
عَنْ ابْنِ أَبِي لَيْلَى أَنَّ قَيْسَ بْنَ سَعْدٍ وَسَهْلَ بْنَ حُنَيْفٍ كَانَا بِالْقَادِسِيَّةِ فَمَرَّتْ بِهِمَا جَنَازَةٌ فَقَامَا فَقِيلَ لَهُمَا إِنَّهَا مِنْ أَهْلِ الْأَرْضِ فَقَالَا إِنَّ رَسُولَ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ مَرَّتْ بِهِ جَنَازَةٌ فَقَامَ فَقِيلَ إِنَّهُ يَهُودِيٌّ فَقَالَ أَلَيْسَتْ نَفْسًا
1250 صحيح البخاري كِتَاب الْجَنَائِزِ إذا رأيتم الجنازة فقوموا
961 صحيح مسلم كِتَاب الْجَنَائِزِ قام النبي صلى الله عليه وسلم وأصحابه لجنازة يهودي حتى توارت
Taken from How should we respond to the death of Mandela? :
It has come to my attention that many people have been quoting an important ḥadīth out of context. It is only for the love of portraying the truth as it should be, are we seeking to correct this misunderstanding, and not simply for the sake of finding the faults of others. The ḥadīth is as follows: Sahl bin Hunaif and Qais bin Sa`d were sitting in the city of Al-Qādisiyyah. A funeral procession passed in front of them and they stood up. They were told that funeral procession was of one of the inhabitants of the land i.e. of a non-believer, under the protection of Muslims. They said, “A funeral procession passed in front of the Prophet (Ṣallāhu ‘alayhi wa salam) and he stood up. When he was told that it was the coffin of a Jew, he said, “Is it not a living being (soul)?”
This narration has been cited by some to prove that showing deep respect and reverence for the deceased is something that is encouraged equally for the disbelievers and believers alike. That is not to say that they should be disrespected or treated unjustly, however, to cite this ḥadith to state that a disbeliever deserves this level of veneration is not accurate.
To clarify, the version of an-Nasā’i states: “A funeral passed by us and the Messenger of Allah stood up and we stood with him. I said: ‘O Messenger of Allah, it is a Jewish funeral.’ He said: ‘Death is something terrifying, so if you see a funeral, stand up,”‘ 
Likewise the version of Ibn Mājah states: “A funeral has passed by the Prophet (Ṣallāhu ‘alayhi wa salam) and he stood up and said: ‘Stand up out of recognition of the enormity of death.’” 
From the above two versions it is clear that the Prophet (Ṣallāhu ‘alayhi wa salam) stood not out of veneration for the dead, but rather for death itself. Likewise, Ibn Ḥajar mentions in Fatḥ that in the version of the incident, as recorded by al Ḥākim, the Prophet (Ṣallāhu ‘alayhi wa salam) said: “We stood out of respect for the Angels (i.e. those that were associated with taking the life of the person)…also in another version the Prophet (Ṣallāhu ‘alayhi wa salam) said: ‘you are only standing out of reverence for the Angels that take the souls of people’ and the version of Ibn Ḥibbān reads: ‘out of awe for Allāh, the one who takes the souls of people.’…” 
Likewise Badr ad-Dīn al ‘Ayni (Rahimahullah [d.855]) said regarding the saying: “Is it not a soul?” ‘He stood due to the difficulty and enormity of death and its remembrance. Thus if he stood up, it made his remembrance of death more affective.” 
Al Munāwi (Rahimahullah [d.1031h]) also stated: “He stood out of respect and honour for the one who took the soul or the angels of punishment that were present or due to the difficulty of death and its remembrance and not for the deceased himself. Therefore, his standing was due to the greatness of the matter of death and to honour the decision and command of Allāh.” 
Many scholars even said that the practice of standing up for the deceased has been abrogated since it was reported that the Prophet (Ṣallāhu ‘alayhi wa salam) sat down afterwards when other processions passed by him!