Reporting from Ikrimah, Ibn Jareer has said that this ayah was revealed in the background of a particular event. After the Prophet sallAllahu aalyhi wa sallam had settled in Madina, he had entered into a treaty with Jews and Christians living nearby which required that they would neither fight against Muslims nor help any other tribe at war with them, rather, they would join Muslims to fight against them.
Similarly, Muslims will not fight them nor help anyone against them, rather, would defend them against the aggressors. For some time, both parties kept adhering to the treaty. But, the Jews could not observe the terms of the treaty any longer because of their conspiratorial nature and anti-Islam temperament. They made a secret deal with the pagans of Makkah against Muslims and wrote them a letter inviting them to their fortress.
When the Prophet sallAllahu aalyhi wa sallam came to know about this conspiracy, he dispatched a posse of Mujahideen to confront them. These Jews from Banu Qurazyzah were, on the one hand, conspiring with the disbelievers of Makkah, while on the other, having infiltrated among Muslims, they had succeeded in making pacts of friendship with many of them. This was their front of spying for the disbelievers of Makkah against Muslims.
Revealed thereupon was this ayah which stopped Muslims from indulging in deep friendship with Jews and Christians, so that they could be deprived of having access to sensitive information about Muslims.
At that time some companions including Ubadah ibn Samit, openly announced the cancellation of their treaty obligations as well as their support for abandonment of any close friendship in the future. As for the hypocrites who had their pragmatic relationship with Muslims, or people whose hearts had yet to taste the sweetness of genuine faith, they apprehended dangers in breaking relationships with Jews and Christians lest the conspiracy hatched by the disbelievers and the Jews succeeded and Muslims were overpowered in which case it would be necessary that they keep their relationships balanced both ways to avoid any problems for them later on. It was on this basis that Abdullah ibn Ubaiyy ibn Salul had said that he saw danger in cutting off relationship with these people and, therefore, he cannot do that. Revealed thereupon was the ayah 52