Reason behind revealing This Ayah, and the prohibition of Betrayal
Allah says;
يَا أَيُّهَا الَّذِينَ امَنُواْ لَا تَخُونُواْ اللّهَ وَالرَّسُولَ وَتَخُونُواْ أَمَانَاتِكُمْ وَأَنتُمْ تَعْلَمُونَ
O you who believe! Betray not Allah and His Messenger, nor betray knowingly your Amanat (things entrusted to you).
The Two Sahihs mention the story of Hatib bin Abi Baltaah.
In the year of the victory of Makkah he wrote to the Quraysh alerting them that the Messenger of Allah intended to march towards them. Allah informed His Messenger of this, and he sent a Companion to retrieve the letter that Hatib sent, and then he summoned him. He admitted to what he did.
Umar bin Al-Khattab stood up and said, "O Allah's Messenger! Should I cut off his head, for he has betrayed Allah, His Messenger and the believers?"
The Prophet said,
دَعْهُ فَإِنَّهُ قَدْ شَهِدَ بَدْرًا وَمَا يُدْرِيكَ لَعَلَّ اللهَ اطَّلَعَ عَلَى أَهْلِ بَدْرٍ فَقَالَ
اعْمَلُوا مَا شِيْتُمْ فَقَدْ غَفَرْتُ لَكُم
Leave him! He participated in Badr. How do you know that Allah has not looked at those who participated in Badr and said, Do whatever you want, for I have forgiven you.
However, it appears that this Ayah is more general, even if it was revealed about a specific incident. Such rulings are dealt with by their indications, not the specific reasons behind revealing them, according to the majority of scholars.
Betrayal includes both minor and major sins, as well those that affect others. Ali bin Abi Talhah said that Ibn Abbas commented on the Ayah,
وَتَخُونُواْ أَمَانَاتِكُمْ
(nor betray your Amanat),
"The Amanah refers to the actions that Allah has entrusted the servants with, such as and including what He ordained. Therefore, Allah says here,
لَا تَخُونُواْ
(nor betray...), `do not abandon the obligations."'
Abdur-Rahman bin Zayd commented, "Allah forbade you from betraying Him and His Messenger, as hypocrites do."
Allah said