To begin to understand this, consider Qur'an 3:28:
Let not the believers Take for friends or helpers Unbelievers rather than believers: if any do that, in nothing will there be help from Allah: except by way of precaution, that ye may Guard yourselves from them. But Allah cautions you (To remember) Himself; for the final goal is to Allah.The Muslim world's most respected Quran expositor, Ibn Kathir, has this to say in explanation:
The Prohibition of Supporting the Disbelievers
Allah prohibited His believing servants from becoming supporters of the disbelievers, or to take them as comrades with whom they develop friendships, rather than the believers. Allah warned against such behavior when He said,
(And whoever does that, will never be helped by Allah in any way)[part of Qur'an 3:28]meaning, whoever commits this act that Allah has prohibited, then Allah will discard him. Similarly, Allah [the Qur'an] said,
(O you who believe! Take not My enemies and your enemies as friends, showing affection towards them),until,
(And whosoever of you does that, then indeed he has gone astray from the straight path.) [Qur'an 60:1].Allah said,
(O you who believe! Take not for friends disbelievers instead of believers. Do you wish to offer Allah a manifest proof against yourselves) [Qur'an 4:144],and,
(O you who believe! Take not the Jews and the Christians as friends, they are but friends of each other. And whoever befriends them, then surely, he is one of them.)[Qur'an 5:51].
Allah said, after mentioning the fact that the faithful believers gave their support to the faithful believers among the Muhajirin [those who moved with Muhammad to Medina], Ansar [people from Medina who helped Muhammad] and Bedouins,
(And those who disbelieve are allies of one another, (and) if you do not behave the same, there will be Fitnah and oppression on the earth, and a great mischief and corruption.) [Qur'an 8:73].
Allah said next [in Qur'an 3:28, which many Islamic scholars say was produced after the above verse, 8:73]"We smile in the face of some people although our hearts curse them." A deception "allowed until the Day of Resurrection." Thus you have a mainstream pious Islamic idea of "friendship" with non-believers.
(unless you indeed fear a danger from them)meaning, except those believers who in some areas or times fear for their safety from the disbelievers. In this case, such believers are allowed to show friendship to the disbelievers outwardly, but never inwardly. For instance, Al-Bukhari [compiler of the most canonical hadith collection] recorded that Abu Ad-Darda [a companion and pupil of Muhammad] said, "We smile in the face of some people although our hearts curse them.'' Al-Bukhari said that Al-Hasan said, "The Tuqyah [or taqqiyah, deception on behalf of Islam] is allowed until the Day of Resurrection.''