All perfect praise be to Allaah, The Lord of the worlds. I testify that there is none worthy of worship except Him, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
The Prophet, sallallaahu ‘alayhi wa sallam, has taught us how to deal with rulers who do not act according to the rulings of the Sharee‘ah (Islamic jurisprudence). He told us that towards the end of time, there would be leaders who would not follow the Sharee‘ah and whom the Muslims would recognize as sinful. Those among them who disapprove [of their deeds] would be spared and those who agree [with them] are in the wrong. The Companions inquired if these rulers should be fought. The Prophet, sallallaahu ‘alayhi wa sallam, replied that war was not to be waged on them, so long as they performed the [obligatory] prayers. [Muslim]
Imaam An-Nawawi described this Hadeeth as miraculous, because the Prophet, sallallaahu ‘alayhi wa sallam, foretold future events that have indeed come to pass exactly as he said.
There are two narrations of this Hadeeth, which differ slightly in terms of their wording, but carry almost the same meaning. One of these narrations reads that those who disapprove [of their actions] would be spared, indicating that a Muslim who condemns their violation of the Sharee‘ah will be safe from the punishment of Allaah The Almighty. However, this only applies to one who cannot actually change the evil with his hand [i.e., practically] or tongue [i.e., by speaking against it].
Another report simply says that the one who recognizes [their actions to be evil] will be safe; this means that those who are aware that the rulers are infringing the rulings of Sharee‘ah and that it is sinful, will be free from the retribution of Allaah The Almighty. This realization may lead them to try to rectify such wrong by the hand, tongue or even with [disapproval in] the heart.
As for the saying of the Prophet, sallallaahu ‘alayhi wa sallam, about one approving of and following [corrupt rulers] being sinful themselves, it implies that if a person cannot change an evil act or speak out against it, this does not render him errant, unless he also endorses it or follows those leaders in their violation of the Sharee‘ah.
One of the rulings also derived from this Hadeeth is that the injustice or immorality of heads of state does not justify fighting them, as long as they do not modify any of the established principles of Islam. This is attested to by the question posed by the Companions about whether the [corrupt] rulers were to be fought. In response, the Prophet, sallallaahu ‘alayhi wa sallam, stated that it is prohibited for anyone to fight them, as long as they offered the prayers.
This was the answer to your question.
Allaah Knows best.