If a person commits a mistake during Prayer, which affects the validity of the Prayer, e.g. if he is in doubt whether he has offered the prescribed number of Rak‘at, the Prostrations of condonement are necessary.
The Prostrations are offered after the recitation of Tashahhud, and Durud, and other prescribed prayers in the final Qa‘dah of the Prayer. Thus, after saying Allahu Akbar, two prostrations are performed, in which Subhana Rabbiyal A‘la is recited, then the Imam reverts back to Qa‘dah position and says Assalamu ‘Alaikum wa Rahmatullah turning his face towards the right and then towards the left, to mark the end of the Prayer.
If the Imam commits such a mistake which can be condoned by the Prostrations, then the whole congregation will have to perform those Prostrations of condonement. But if one of the followers commits a mistake while following the Imam, he is not required to perform the Prostrations of condonement.
If there is a doubt as to how many Rak‘at have been performed, then one should observe the rule of certainty, i.e. if the doubt is whether one has offered three or four Rak‘at, for instance, one should offer the fourth Rak‘at to be on the safe side though one might have offered it before.