If the imam stands up for a fifth rak’ah by mistake, the people praying behind him should alert him so that he can sit back down. If he does not sit back down because he thinks that he is right, it is not permissible for the one behind him who knows that it is a fifth rak’ah to follow the imam and stand up with him, because by doing that he will have added a rak’ah to his prayer, knowingly and deliberately, which invalidates the prayer.
Rather the one who is praying behind him should sit and recite the tashahhud then say the salaam, or wait for the imam and say the salaam with him.
Shaykh al-Islam Ibn Taymiyah was asked about an imam who stood up for a fifth rak’ah and the people said Subhaan-Allaah but he did not pay any attention to them and he thought that he had not made a mistake. Should they stand up with him or not?
If they stand up with him because they did not realize (it was a mistake) then their prayer is not invalidated, but if they know then they should not follow him, rather they should wait for him until he says the salaam, or say the salaam before him, but it is better to wait. End quote.
Majmoo’ al-Fataawa (23/53).
It says in Fataawa al-Lajnah al-Daa’imah li’l-Ifta’ (7/128):
As for the one who is praying behind an imam and is certain that the imam is doing an extra rak’ah – for example – it is not permissible for him to follow him in that; if he follows him knowing that it is extra and knowing that it is not permissible to follow him, then his prayer is invalidated.
But if he does not realize that it is extra, then he may follow him, and this also applies if he does not know of the ruling. End quote.
It also says (1/132):
If a person in the congregation knows that his imam has stood up to offer an extra rak’ah such as a fifth rak’ah in a four-rak’ah prayer, he should say tasbeeh. If the imam sits back down, all well and good, otherwise he should sit and wait for the imam, and say the salaam with him. End quote.
And Allaah knows best.