Praise be to Allaah.
If the imam was sure that he was right, and he did not pay any attention to the alert from the people praying behind him, and he completed his prayer and had prayed five rak’ahs, then his prayer is valid and he does not have to do anything else. Then if he finds out after the salaam what had happened, he should prostrate twice (the prostration of forgetfulness) then say the salaam.
If the person who is praying behind an imam realizes that his imam has stood up for an extra rak’ah, he has to alert him to that. If he does not respond, it is not permissible for him to follow him, rather he should stop doing so and sit and recite the final tashahhud and say the salaam. If he follows him knowing that this is the fifth rak’ah, then his prayer is invalid. But if he follows him not knowing about that or forgetting, then his prayer is valid.
It says in Sharh Muntaha al-Iraadaat (1/223): If a person makes a mistake but is alerted by two trustworthy persons, then he must stop, unless he is certain that he is right, in which case it is not permissible for him to stop.
Then he said that if he stands up for a fifth rak’ah, it is not permissible for the person praying behind him to follow him because he believes that he is wrong and what the imam stood up for is not part of his prayer. But if he follows him out of ignorance or because he forgot, or he stops following him, then his prayer is valid. The one who knows what is happening must stop following him, and the one who has stopped following his imam should say the salaam after he stands up for the extra rak’ah and then alert him when he has completed the final tashahhud. End quote.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) was asked: If the imam prays five rak’ahs by mistake, what is the ruling on his prayer and the prayer of those behind him? Should the one who joined the prayer late count this additional rak’ah (as part of his own prayer)?
He replied: If the imam prays five rak’ahs by mistake, then his prayer is valid, and the prayer of those who follow him by mistake or out of ignorance is valid too.
But if a person knows that it is an extra rak’ah, when the imam stands up for the extra rak’ah he should sit and say the salaam, because in this case he believes that his imam’s prayer has become invalid, unless he is afraid that his imam has stood up for an extra rak’ah because he failed to recite al-Faatihah (for example) in one of the rak’ahs. In that case he should wait and not say the salaam (i.e., he should wait to say the salaam with the imam).
With regard to the one who joined the prayer late and joined the imam in the second rak’ah or later, then this extra rak’ah counts for him. If he joined the imam in the second rak’ah, for example, he should say the salaam with the imam when he does an extra rak’ah. If he joined in the third rak’ah, he should do another rak’ah after the imam says the salaam following the extra rak’ah. That is because if we say that the one who joined the prayer late should not count the extra rak’ah, then he would have to add another rak’ah deliberately, and that would make his prayer invalid. As for the imam, he is excused for the extra rak’ah, because he forgot, so his prayer is not invalid. End quote from Majmoo’ Fataawaa l-Shaykh Ibn ‘Uthaymeen (14/19).