There are various traditions on the reasons for revelation of this verse.
The closest is the one mentioned in the ahadith Bukhari 4591 and Muslim 3025 and Jami at-Tirmidhi 3030 etc. A group of the Muslims were on a military expedition in the Dar al-Harb. They came to a settlement in which there was only one Muslim and the rest were all disbelievers. When the army came close to the settlement the disbelievers ran away and the Muslim inhabitant also ran away with his sheep since he did not know that it was a Muslim army. However when he heard their Takbir he recognized them and came back to them. The man greeted them with the Islamic greeting, hinting that he too was a Muslim in hiding. However the Muslims in the army suspected that he was deceiving them in order to save himself and his property. Hence they killed him and took his sheep as war booty. On this Allah revealed the verse where he commanded the Muslims to not pass judgement based on their suspicions - rather that they should have 'investigated' the matter - and after that they should have taken the victim on his words and not suspected his intentions.
Another tradition is that this was revealed regarding the action of Usama bin Zaid (his expedition is recorded in Bukhari 6872 and Muslim 96b). During a battle Usama overpowered his opponent who then accepted Islam by reciting the shahada. However Usama assumed that he was only pretending in order to save himself, hence Usama killed him.
There are also other traditions with minor variations and details. For example some state that this was revealed in response to the action in battle of Muhallim bin Jaththamah or in response to a hypothetical question asked by Miqdad bin Al Aswad. Some have noted that the victim said 'I am a Muslim', or that he said all three "Assalamu alaikum" , "I am a Muslim" and "There is no god except Allah and Muhammad is His Messenger". And some traditions note that there was some previous personal enmity between the victim and his killer due to which the killer did not pay attention to his acceptance of Islam. And some traditions state that the killer was punished by Allah by death and then expulsion of his body from his grave. And some state that the Prophet ﷺ ordered payment of blood money and return of his property to the family of the victim, while others state that the killer was told to pay Kaffarah by freeing a Muslim slave.
Explanation of the verse:
O you who have believed, when you go forth [to fight] in the cause of Allah,
'The cause of Allah' means Jihad. This means that this event happened during a military excursion into the Dar al-Harb.
See the explanation below at the second occurrence of this at the end of the verse.
and do not say to one who gives you [a greeting of] peace
This means the islamic greeting of "Assalamu alaikum" (peace be upon you), which is what the victim said according to the first tradition. This greeting was a habit of the Muslims and not usual for the polytheists so it raises the possibility that the speaker is a Muslim, or that he indirectly wants to inform the people that he is a Muslim.
Alternately it means pretense of Islam by reciting the shahada as in the second tradition. Or it means saying that "I am a Muslim" as in some other traditions.
(do not say) ... "You are not a believer,"
If the opponent gives an impression that he is (or has now become) a Muslim - then do not accuse him of being a disbeliever in reality. Nor assume that he is being deceitful based on suspicion alone. Rather investigate the matter if it is unclear, and if the opponent clearly claims to be a Muslim then accept it and spare him.
aspiring for the goods of worldly life; for with Allah are many acquisitions.
This alludes to war booty, personal revenge or other material gains. Since these are the desires which cause one to be hasty in judgement and to act on mere suspicions about the victim.
You [yourselves] were like that before
This could mean either of two things:
You too used to conceal your belief and lived among the disbelievers in the Dar al-Harb - just like the victim. That is because initially the Muslims lived in Mecca before the migration, and many of them hid their belief in order to avoid persecution.
So the Muslims should keep in mind that it is possible for there to be a Muslim living among the disbelievers in the Dar al-Harb who hides his belief and only reveals it when faced with a Muslim expedition.
إذا كان رجل مؤمن يخفي إيمانه مع قوم كفار، فأظهر إيمانه، فقتلته، فكذلك كنت أنت تخفي إيمانك بمكة من قبل
If a faithful believer conceals his faith (Islam) from the disbelievers, and then when he declares his Islam, you kill him, (you will be sinful). Remember that you were also concealing your faith (Islam) at Mecca before.
You too were disbelievers initially before your acceptance of Islam. So your state is similar to the state of the victim who accepted Islam.
then Allah conferred His favor upon you.
This favor could mean that Allah gave you military strength so that you could openly divulge your belief in Islam. Otherwise it means that Allah gave you guidance to accept Islam after your initial state of disbelief.
Investigate before killing in war. This happens in the following cases:
If the victim greets with the Islamic greeting or shows some other vague sign of being a Muslim. Here one should investigate to find out whether the victim is really a Muslim or whether he is a disbeliever.
If the victim says that he is a Muslim or recites the shahada then there are two opinions:
One should just accept these statements on their face value and consider him as a Muslim. It is not possible to investigate what a person hides in his heart.
One could investigate what the victim means by these words since sometimes disbelievers attach their own meaning to these statements. For example one might think that their own religion is in reality Islam. Or they might accept Muhammad ﷺ as a Messenger but do not feel the need to follow his teachings.
So the victim should be asked to clarify what he means. If he means acceptance of the religion brought by Muhammad ﷺ as his religion then his word should be accepted on its face value and he can not be executed based on suspicion.
Killing a Muslim is forbidden, even if he accepts Islam after being defeated and captured. Whereas killing a harbi disbeliever is permissible - even if it is after the opponent surrenders or is captured (8:67, Tirmidhi 1584) or claims to want peace (9:1, 9:7, 47:35, 8:58).
أمرت أن أقاتل الناس حتى يشهدوا أن لا إله إلا الله وأن محمدا رسول الله ... فإذا فعلوا ذلك عصموا مني دماءهم وأموالهم
I have been ordered (by Allah) to fight against the people until they testify that none has the right to be worshipped but Allah and that Muhammad is Allah's Messenger ... if they perform that, then they save their lives and property from me.
Indeed Allah is ever, with what you do, Acquainted.
Meaning fear Allah in this matter.
For further reference see: Tafsir al-Qurtubi, Tafsir al-Jassas , Tafsir Ibn Kathir, Tafsir al-Baghawy, Tafsir al-Tabary. Also see the related question: Did Qurtubi really say “if a muslim meets a kafir with no covenant, it is permissible for him to kill him”?