The verse that you have asked about is often used by some anti-Islam supporters to portray Islam as a hateful and intolerant religion which goes against peaceful co-existence.
They use the verse to say that a Muslim isn’t allowed to be friends with non-Muslims. It is correct that some translations have translated the word ‘Awliya’ - the plural of wali’ - as ‘friends’ but the word wali or awliyah, can have various meanings depending on the context.
Firstly in Islam as we know, marriage is allowed with the people of the book. Therefore it wouldn’t make sense for Allah to command us not be friends with the people of the book and at the same time, allow marriage with them. Is it possible that one may have a Christian or Jewish wife but not be her friend?!
Secondly, when we read history, we find that the prophet Mohammad had (sawa) peaceful interactions with the people of the book as well as friendship.
Extracted from a longer Hadith in Kitāb Al-Tawhīd by Sheikh Saduq, we find the following:
كان لرسول الله صديقان يهوديان
“The Messenger of Allah had two Jewish friends”
This shows us that according to our books, the prophet did in fact have friendship with those from the people of the book. By coupling this hadith with the verse, we would come to realise that the word ‘wali’ must have a different meaning than friend.
When we look into some books of history, we find that according to some reports, this verse was revealed during an incident relating Jews who had breached a covenant with the prophet.
Please read the passage below:
“It is reported that the Messenger of Allah besieged them for six days until finally they surrendered to him. `Abdullah ibn Ubayy came to him and interceded on their behalf saying: “O Apostle of Allah, these are my allies and clients who have defended me against the black and the red (that is, against all kinds of people). They were three hundred armoured soldiers and four hundred without armour. Would you now cut them down all in one morning? By Allah, I can then find no security; rather I dread the turns of for- tunes!” The people of Banu Qaynuqa` were allies of the Khazraj tribe only, and not of the Aws. `Abdullah ibn Ubayy persisted in his entreaties until the Prophet relented and granted him their blood. But seeing the humiliation which they had suffered, the people of Banu Qaynuqa` left Medina altogether and settled in Adhri'at in Syria. Then Allah sent down concerning `Abdullah ibn Ubayy and others of the Khazraj tribe:
O you who have faith, take not the Jews or the Christians as patrons. . . (Qur'an. 5 : 51‐ 52) .”
Source: Beacons of Light: Muhammad, the Prophet and Fatimah az-Zahra’ (the Radiant) a Partial translation of I'lamu 'l Wara bi Alami 'l-Huda of Abu Ali al Fadl ibn al Hasan ibn al Fadl at Tabarsi (c. 468/1076 - 548/1154).
So we see that from this source, a hypocrite from among the companions, tried to intercede for the Jews of Banu Qaynuqa who broke the peace treaty with prophet which was a betrayal. The Prophet allowed this Jewish tribe to reside in Medina on the condition that they do not help enemies against the Muslims; despite this, they still broke their agreement.
From this source it shows that Allah revealed this verse to rebuke the hypocrite Abdullah bin Ubay who took these people as his allies and his protective guardians. He relied on them and had a type of attachment towards them over the prophet and Muslims.
This leads us understand that if we take the verse to mean friends, it would indicate those from them who Muslims have conflicts with, and not regular people from Ahlul kitab.
However from the context of the historical report, it seems that the correct translation could be the following:
O you who have faith, take not the Jews or the Christians as patrons or protective guardians. . . (Qur'an. 5 : 51‐ 52)
Also it could be possible for a Muslim to be forbidden from friendship with some other Muslims. For example if they would lead him astray or to an immoral path, he must not be friends with them despite them being Muslims. So this isn’t discriminatory and restricted to only people of the book.
If a Muslim was to have a Christian friend for example, who he may study with or partake in sports with etc, then this would be permitted providing that this person wouldn’t lead him down an immoral path or a deviated path. How many times have we found ourselves around Christians who held better moral traits than Muslims although they are upon Batil?
Also to mention, it is better for someone to take mu’minin as friends as this will lead them to become stronger in iman and prevent them from haram things.
It should be noted that the book above, also contains reports from non-Shia reported Seerah of the prophet. This is due to the Shia losing many books by having them burnt by the enemies, so the above opinion is a possible exegesis for the verse and not 100% definite. Allah swt knows best and the full explanation of the Quran will be with us when Imam Mahdi (May Allah hasten his reappearance) returns.
What we do know for certain, is that it wouldn’t mean friends in the absolute sense as the Prophet had two Jewish friends as reported in our sources. As we know, the Prophet Mohammad (sawa) is of course our role model who we try to imitate. If he showed friendship and kindness towards the non-Muslims, we may also take this example.
May Allah grant you success