If this imam has repented and changed his ways for the better, then there is nothing wrong with appointing him as an imam to lead others in prayer, or with praying behind him. No one has the right to criticize him or punish him for a sin from which he has repented. Allaah says (interpretation of the meaning):
verily, Allaah forgives all sins
i.e., for the one who repents.
The Prophet (peace and blessings of Allaah be upon him) said: The one who repents from sin is like one who did not sin at all. Narrated by Ibn Maajah, 4250; classed as hasan by al-Albaani in Saheeh Ibn Maajah.
But if he is still committing that great evil, he must be advised and warned and made to fear Allaah. If he does not repent, then efforts must be made to prevent him from leading the prayer and he should be replaced with another, righteous imam who adheres properly to his religion and is keen to attain the quality of taqwa. The Prophet (peace and blessings of Allaah be upon him) forbade a man to spit in the direction of the qiblah whilst praying, and he forbade him to lead his people in prayer, and he said to him: You have offended Allaah and His Messenger. Narrated by Abu Dawood, 481 and classed as hasan by Shaykh al-Albaan in Saheeh Sunan Abi Dawood. So how about one who commits this great evil? He is more deserving of being prevented from leading the prayers!
Moreover, appointing him to lead the prayers is a form of showing respect to him, and an evildoer is not deserving of such respect.
The imam is usually someone whom the people take as an example and from whom they learn and receive guidance. The more just and righteous he is, the more that will benefit the people and make them more likely to accept what he says.
But if he is an evildoer, the people will not accept anything from him, rather he may be the cause of some of them being led astray, Allaah forbid.
With regard to the validity of prayers offered behind an evildoer, the imams (scholars) differed concerning that. The view of the majority is that a prayer offered behind such a person is valid, although it is makrooh.
Al-Nawawi said in al-Majmoo', 4/151:
It is proven in Saheeh al-Bukhaari that Ibn Umar prayed behind al-Hajjaaj ibn Yoosuf, and many other saheeh ahaadeeth indicate that prayers offered behind evildoers and unjust rulers are valid.
Our companions said: Prayers offered behind evildoers are not haraam, but they are makrooh. It is also makrooh to pray behind an innovator whose bid'ah does not reach the degree of kufr, but such prayers are valid. But if his bid'ah constitutes kufr then as we have stated before, prayers offered behind him are not valid as is the case with any other kaafir. Al-Shaafa'i stated in al-Mukhtasar that it is makrooh to pray behind an evildoer or an innovator, but if one does that, the prayer is valid.
Shaykh al-Islam [Ibn Taymiyah] said in Majmoo' al-Fataawa, 23/341:
If there are two men of religious commitment, then whichever of them has more knowledge of Qur'aan and Sunnah should be given precedenceâ€¦ If one of them is an evildoer, such as if he is known for lying or being treacherous and so on, and the other is a believer and is pious, then the latter is more entitled to lead the prayers if he is qualified to do so, but if the former has more knowledge of Qur'aan and is more knowledgeable, then prayer behind the evildoer is emphatically forbidden according to some scholars and is disliked according to othersâ€¦ and it is not permissible to appoint the evildoer when it is possible to appoint the righteous man.
He also said (23/375):
It is not permissible to appoint someone to lead the prayers who consumes hashish or does evil and haraam things when it is possible to appoint someone who is better than him.
The imams are agreed that it is makrooh to pray behind an evildoer, but they differed as to whether such a prayer is valid. It was said that it is not valid, as is the view of Maalik and Ahmad according to one of the two reports narrated from them. And it was said that it is valid, as is the view of Abu Haneefah and al-Shaafa'i, and the other view narrated from Maalik and Ahmad. But there is no dispute concerning the fact that such a person should not be appointed. End quote.
See also al-Sharh al-Mumti', 4/403-308
The scholars have discussed in detail what should be done with an evildoer imam. It says in Fataawa al-Lajnah al-Daa'imah:
Based on this, if he is the imam of a mosque and he does not pay heed to advice, then he must be removed if possible and if that will not lead to fitnah (tribulation), otherwise those who are able to should pray behind someone else who is righteous, as a rebuke to him and a denunciation, so long as that will not lead to fitnah (tribulation).
If it is not possible to pray behind someone else, then it is prescribed to pray behind him, so as to preserve the interests of the jamaa'ah (group or congregation).
If there is the fear that praying behind someone else will provoke fitnah, then (the people) should pray behind him so as to ward off fitnah and so as to do the lesser of two evils.
From Fataawa al-Lajnah al-Daa'imah, 7/370
And Allaah knows best.
Reproduced from Islam QA