There is no doubt that the Prophet (peace be upon him) said – as related by `Abd Allah b. `Umar – “Whoever dies without being bound by the oath of allegiance (bay`ah), dies the death of the time of ignorance.” [Sahîh Muslim (1851)]
When there is a legitimate head of state for the Muslims (imam) who is recognized by Islamic Law, then it is not permissible to a Muslim to refrain from recognizing him. This is what the hadîth means.
When Imam Ahmad was asked about the meaning of this hadîth, he replied: “Do you know what an imam is? The imam is the one around whom the Muslims are united. All of them say that he is the imam, and this is what it means.” [This statement was related by Muhammad b. Ishâq in his Masâ’il (2/185) and by al-Khallâl in al-Imân]
This oath of allegiance (bay`ah) is directly incumbent upon the leaders of the Muslim community (ahl al-hall wa al-`aqd) and must be given on the authority of the Qur’ân and Sunnah according to the conditions set forth in Islamic Law. As far as the general public is concerned, most scholars agree that the pledge of allegiance given by their community leaders will suffice them, so it is not necessary for every single individual to do so. This is what Abû Ya`lâ says in his work al-Mu`tamad (p. 254) and in his al-Ahkâm al-Sultâniyyah (p. 27). It is also the opinion stated by al-Mâwardî in his book by the same name (p. 15).
Even though the individual in this case does not give the oath of allegiance directly, he is bound by it. He is required to obey in all matters that do not entail disobedience to Allah.
Under circumstances where there is no imam as defined by Islamic Law, or where allegiance is demanded on something other than the Qur’ân and Sunnah, then it is not permissible to offer the oath of allegiance. This is clear from the following hadîth related by Hudhayfah b. al-Yamân:
Allah’s Messenger (peace be upon him) said: “There are those who invite from the gates of Hell, and anyone who accepts their invitation will be cast therein.”
I said: “O Messenger of Allah, describe them to us.”
He said: “They are people from our own country who speak our language.”
I asked: “So what do you bid me to do if I come across them?”
He said: “Stay with the community of Muslims and their imam. If the Muslims have no community and no imam, then turn away from all of those sects, even if means you having to adhere to the trunk of a tree until death overtakes you.” [Sahîh al-Bukhârî and Sahîh Muslim]
The point of this is that the oath of allegiance (bay`ah) is not obligatory except when the conditions for it are met – like the existence of a recognized imam for the Muslims – and in the absence of factors that legally prevent it from being obligatory. However, it does not matter, with respect to the obligation of offering one's allegiance, if the imam exhibits sinful or oppressive qualities.
The hadîth does not mean, as the questioner seems to have understood, that a person must give his allegiance to anybody, no matter who they are, in order to avoid the negative consequences mentioned in the hadîth.
The proof for this is that the narrator of the hadîth, Ibn `Umar – who knew its meaning better than anyone and was very committed to putting it into practice – went for a period of time without giving his oath of allegiance to anyone.
The first time he refrained from offering his oath of allgiance was during the period of disagreement between `Alî and Mu`âwiyah. He remained in this way until Mu`âwiyah made peace with al-Hasan and the people gave their oath of allegiance to him. He then gave his oath of allegiance to Yazîd after the death of his father Mu`âwiyah, because the people had united around him.
He again refrained from giving an oath of allegiance to anyone during the disagreement that followed until Ibn Zubayr was killed. When the government was again united under `Abd al-Malik b. Marwân, Ibn `Umar again offered his oath of allegiance. [Refer to Fath al-Bârî (13/195) and al-Bidâyah wa al-Nihâyah (3/30)]