All perfect praise be to Allah, The Lord of The Worlds; and may His blessings and peace be upon our Prophet Mohammad and upon all his family and companions.
If an Imaam stood up to perform an additional Rak`ah out of forgetfulness, but remembered offering it, then he should return to Tashahood and perform the two prostrations of forgetfulness. The Ma`moomeen should warn him, so if he was doubtful about the additional Rak`ah, then he isn`t allowed to go back to Tashahood unless they were too many (They warned him that he is standing up to offer an additional Ra`kah).
If he didn`t go back to Tashahood, then his prayer is invalid and it is forbidden for the Ma`moomeen to follow him because if they did, their prayer becomes invalid as well. However, if they have split and didn`t agree on one opinion, then the Imaam relies on what he thinks is most likely, namely the lesser number of Rak`ahs, and disregards them.
Those who are certain that the Imaam is standing up to perform an additional Rak`ah are forbidden from following him, so they either offer Tasleem or keep sitting and offer Tasleem with him, which is better. If they followed him in offering a fifth Rak`ah, then their prayer is invalid. However, those who aren`t certain that he is standing up to perform an additional Rak`ah, should follow him in what he thinks is most likely since he is there to be followed.
As for those who arrived late and didn`t realize that the Imaam was standing up to offer an additional Rak`ah, this extra Rak`ah counts for them. However, if they realized that it was an additional Rak`ah, then they shouldn`t follow him in order to make up their missed Rak`ahs since following him will render their prayer invalid.
In conclusion, the prayer of those who performed a fifth Rak`ah knowingly is considered invalid; whereas,the prayer of those who kept sitting for Tashahood until the Imaam finished the fifth Rak`ah, or offered Tasleem is considered valid. As regards the Imaam, if he was certain that he had performed a fifth Rak`ah (an extra Rak'ah), then his prayer is considered invalid. And Allah knows best.