Shaykh Bin Baz wrote:
All Imams agreed upon that and that it is not a must to know the Imam's creed before praying behind him or before examining him by asking him what is your creed?
One can pray behind some one whose creed we do not know. It is permissible to pray behind someone who has and calls for a Bid`ah or a sinful person if he is the regular Imam whom we have to pray behind like the Imam of Jumu`ah (Friday) and the two Salat-ul-`Eid (the Festival Prayer) and the Imam in the Hajj prayer in `Arafah and the like. In this case, most of the Salaf (righteous predecessors) and those who came after see that we have to pray after him. Whoever abandons the Friday and congregational prayers behind the sinful Imam commits an act of Bid`ah according to the view of the majority of scholars. The right act is to pray behind this Imam and not to repeat the prayer separately as the companions (may Allah be pleased with them) prayed the Friday and congregational prayers behind the sinful person and did not repeat them as the case of `Abdullah ibn `Umar who prayed behind Al-Hajjaj ibn Yusuf and as the case of Anas ibn Malik (may Allah be pleased with him) as we mentioned before and `Abdullah ibn Mas`ud (may Allah be pleased with him) and others prayed behind Al-Walid ibn `Uqbah ibn Abu Mu`ayt who drank wine till he got drunk and prayed the Fajr prayer four Rak`ah (unit of Prayer) one day and said shall I increase the number of Rak`ah (unit of Prayer)? Ibn Mas`ud said to him today you have really increased them. It is authentically reported that when `Uthman (may Allah be pleased with him) was surrounded, the people prayed behind an Imam then someone asked `Uthman: You are the Imam and this one who prayed as Imam is an Imam of sedition, `Uthman said: O my brother's son, prayers are the best deeds of men so if the Imams pray it well, pray well behind them, and if they do wrong, avoid their wrong deeds.
The prayer of someone who commits Bid`ah and a profanity is valid, so if the people prayed behind them, their prayers are valid but people hate to pray behind them, for enjoying the good and forbidding the evil is a must.
For example, if he openly discloses his religious innovations or sins, he should not be appointed as a regular Imam. He deserves to be rebuked till he repents and if it is possible to desert him till he repents that would be good. If some people do not pray behind him and that would help to forbid the evil till the Imam repents or is fired and people abandon the sins he commits, then it will be good, for the benefit of the religion is not to pray behind him but taking care not to miss Friday or congregational prayers.
However, if the Ma'mum, by doing so, will miss the Jumu`ah and the congregational Salah, his act will be that of a Mubtadi` and thus contradicting the conduct of the Companions (may Allah be pleased with them). Also if the Imam is appointed by those in authority and there is no Shar`y benefit in not performing Salah behind him, it will not be permissible to leave Salah behind him. Rather, offering Salah behind him will even be preferable. Thus, if one can remove an aspect of evil concerning leading Salah, it is incumbent to do so. Yet, if such Imam is appointed by those in authority and one cannot stop him from leading people in Salah, or that one can stop him doing so but with greater harm, it will not be permissible to get rid of little harm by incurring a greater degree of it. Also, it is not permissible to get rid of the lesser of two evils by doing the greater of the two. The divine laws are meant to achieve and protect interests, and to eliminate evils or reduce them as much as possible. Not performing Jumu`ah and congregational Salah has greater harm than offering Salah behind a corrupt Imam, especially when missing them does not lead to the removal of corruption. In such case, the Shar`y benefit will be thwarted without removing the evil.
However, if one can pray Friday and congregational prayers behind a pious Imam, of course that would be better and if one prays behind a sinful Imam in this case, a good deal of controversy would arise as some scholars see that he has to repeat the prayer while others say he do not have to.