Praise be to Allaah.
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).
And Allah knows best.