Praise be to Allah.
He should say the salaam with the imam, because his prayer is complete. With regard to the imam, he is excused for this extra rak’ah.
Shaykh Ibn ‘Uthaymeen 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 a latecomer count this extra rak’ah?
He replied: if the imam prays five rak’ahs by mistake, his prayer is valid, and the prayer of those who followed him by mistake or out of ignorance is also valid.
But if a person knew that this was something extra, when the imam stood up for the extra rak’ah, he should have sat and said the salaam, because in this case he thought that the prayer of his imam was invalid, unless he feared that his imam stood up for the extra rak’ah because he did not recite al-Faatihah properly (for example) in one of the two rak’ahs. In that case he should wait and not say the salaam.
With regard to the latecomer who joined the imam in the second or subsequent rak’ah, this extra rak’ah counts for him. So if he joined the imam in the second rak’ah, for example, he should say the salaam with the imam who prayed an extra rak’ah. If he joined in the third rak’ah, then he should do another rak’ah after the imam says the salaam following the additional rak’ah. That is because if we say that the extra rak'ah does not count for the latecomer, he would have to add another rak’ah deliberately, which would mean that the prayer was invalid. As for the imam, he is excused for the extra rak’ah, because he forgot (and did it by mistake), so his prayer is not invalidated.
Majmoo’ Fataawa Ibn ‘Uthaymeen, 14/20.