Allegiance (bay’ah) is a pledge to obey. And it is a shar’i contract between the one who swears allegiance and the one to whom allegiance is sworn, namely the ruler or caliph.
Allegiance is sworn to a caliph after the decision makers (ahl al-hill wa’l-aqd) select him; they are the ones who meet the conditions of being trustworthy and of sound opinion.
In al-Mawsoo’ah al-Fiqhiyyah (9/274) it says:
Bay’ah or allegiance, as defined by Ibn Khuldoon in his Muqaddimah, means: a pledge to obey; it is as if the one who swears allegiance is promising his ruler that he will accept his authority with regard to his own affairs and the affairs of the Muslims, and he will not dispute with him with regard to any of that, and he will obey him in any duties that he assigns to him, whether at times of ease or at times of hardship. When they swore allegiance to a ruler and made this pledge, they would put their hands in his as confirmation of the pledge. That is akin to what the seller and purchaser do, so bay’ah or allegiance was accompanied by a handshake. End quote.
It also says (9/278):
The selection by the decision makers of the ruler and their swearing allegiance to him (bay’ah) is the basis of his taking that position. The decision makers (ahl al-hill wa’l-‘aqd) are the scholars and people of wisdom and high status, whose knowledge is accompanied by other essential conditions: trustworthiness, good character and wisdom. End quote.
Just as there are conditions that must be met by the decision makers, there are also conditions that must be met by the caliph to whom allegiance is sworn. Some of these conditions are the subject of scholarly disagreements, but on others there is consensus. Not one of the scholars disagreed with the condition that the ruler must be a Muslim, because what is implied by bay’ah (swearing allegiance) is the implementation of the laws of Allaah, carrying out of hadd punishments and guarding the borders of Islam, so how can a kaafir implement the laws of Allaah and do these things? Rather if the ruler is a Muslim and becomes a kaafir, he must be removed from office because of his kufr.
Ibn Hazm (may Allaah have mercy on him) said, discussing the conditions of leadership:
He should be a Muslim, because Allaah says (interpretation of the meaning): “And never will Allaah grant to the disbelievers a way (to triumph) over the believers” [al-Nisa’ 4:141]. Rulership is the greatest of such ways. And Allaah has enjoined us to put the people of the Book in their place and to take the jizyah from them. End quote.
Al-Fasl fi’l-Milal wa’l-Ahwa’ wa’l-Nihal (4/128).
Al-Nawawi (may Allaah have mercy on him) said:
Al-Qaadi said: The scholars are unanimously agreed that a kaafir should not be appointed as ruler, and that if the ruler becomes a kaafir, he must be deposed. End quote.
Sharh Muslim (12/229).
In al-Mawsoo’ah al-Fiqhiyyah (6/218) it says:
The fuqaha’ stipulated conditions for the ruler, on some of which there is consensus and concerning others there is a difference of opinion.
The conditions of rulership on which there is consensus are:
1 – Islam (i.e., the ruler should be a Muslim), because this is a condition of testimony being acceptable and guardianship being valid, which are much less serious issues than rulership. Allaah says (interpretation of the meaning): “And never will Allaah grant to the disbelievers a way (to triumph) over the believers” [al-Nisa’ 4:141]. And rulership, as Ibn al-Qayyim said, is the greatest of such ways. And he is to take care of the interests of the Muslims. End quote.
Based on this, it is not permissible to swear allegiance to a kaafir ruler, and if the ruler becomes a kaafir then he should be deposed, according to the conditions that are known to the scholars.
And Allaah knows best.