All perfect praise be to Allah, The Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad is His slave and Messenger.
The hadeeth that you mentioned was reported by Al-Bukhaari and Muslim and narrated by Abu Hurayrah, may Allah be pleased with him; it relates that the Prophet said, “Verily, the Imaam is a shield behind whom you fight and you protect yourself with. So if he commands piety to Allah, The Almighty, and is just, then he receives reward for that, and if he commands otherwise, then it is against himself.”
The interpreters of the hadeeth said:
“‘The Imaam (ruler/leader of the Muslims) is a shield for them’; that is to say like a protection, because he prevents the enemy from harming the Muslims, prevents the people from being unjust to each other, and protects the principles and rituals of Islam, and people fear him and fear his power.
‘Behind whom you fight;’ meaning that people fight with him against the disbelievers, the unjust people and the deviant sects, as well as all evildoers.
‘And you protect yourself with;’ meaning from the harm of the enemy, the dissolute people, and the unjust people.
‘And if he commands otherwise, then it is against himself;’ meaning that if he enjoined or said what is not pleasing to Allah and what is not justice, then the sin of this falls back on him.”
Allah knows best.