Praise be to Allah.
If the imaam is a charlatan who claims to have knowledge of the unseen or who does false and evil actions, then it is not permissible to take him as an imaam or to pray behind him, because whoever claims to have knowledge of the unseen is a kaafir – we ask Allaah to keep us safe and sound. Allaah says (interpretation of the meaning):
“Say: “None in the heavens and the earth knows the Ghayb (Unseen) except Allaah” [al-Naml 27:65]
The same applies to one who practices sihr (witchcraft or magic) – he comes under the same rulings as the kaafir, because Allaah says (interpretation of the meaning):
“They followed what the Shayaateen (devils) gave out (falsely of the magic) in the lifetime of Sulaymaan (Solomon). Sulaymaan did not disbelieve, but the Shayaateen (devils) disbelieved, teaching men magic and such things that came down at Babylon to the two angels, Haaroot and Maaroot, but neither of these two (angels) taught anyone (such things) till they had said, ‘We are for trial, so disbelieve not (by learning this magic from us)’…”
But if he is a person who commits some sins, but he does not do any actions of kufr such as practising witchcraft and claiming to have knowledge of the unseen, then prayers offered behind him are valid, but it is better to find someone else who is of sound character and is righteous, to be on the safe side and to avoid differing with those scholars who say that it is not permissible to pray behind (such a person).
Sinners should not be taken as imaams, but if they are already in that position, prayers offered behind them are valid, because it may be that the people are being tested by means of them, or necessity may force us to pray behind them. But in the case of one who calls upon anyone other than Allaah, or who seeks the help of the dead, we should not pray behind him, because this action makes him one of the kuffaar, for this is the action of the mushrikeen whom the Prophet (peace and blessings of Allaah be upon him) fought against in Makkah and elsewhere. We ask Allaah to reform the Muslims and to grant them understanding of His religion, and to cause the best of them to become their leaders, for He is the All-Hearing, Ever ready to respond.