It has been authentically proven that the Prophet (pbuh) used to occasionally forget while he was in prayer.
He said, instructing his followers:
“I am only a human being, I forget like you forget, so if one of you forgets (in his prayer) he should prostrate twice (extra) in his (final) sitting.” (Ahmad)
How to Perform Two prostrations (sajdah) for Forgetfulness
Prostrate twice either before the taslim (peace greeting) or after it. Both are authentically reported from the Prophet (pbuh.)
“When one of you becomes uncertain in his prayer and doesnt know how many units (rakaat) he prayed, three or four, he should cast away his uncertainty and build on what he is sure about (i.e. the lesser number), then prostrate twice before the taslim.” (Bukhari)
In the incident related to Dhul-Yadain (where the Prophet (pbuh) prayed two units instead of four, then completed the remaining unit after his attention was called to the lapse), he performed sujud As-sahw after the taslim. (Agreed upon.)
It is preferable to try to follow the sunna as closely as possible, performing sujud before the taslim or performing sujud after the taslim in different situations as mentioned in the hadith. In all other situations he has the choice.
The Prophet (pbuh) said:
“If a person added to his prayer or detracted from it, he should perform two sajdah.” (Muslim)
The Situations Which Call for sajdah As-sahw
1. If one makes taslim before the completion of the prayer, he should stand, complete the remainder and then perform two sajdah at the end of the prayer.
2. If one adds something to the prayer. The proof follows: The Prophet (pbuh) once prayed five rakaat. He was asked, “Has an addition been made to the prayer?” He said, “Why do you so ask?” They said, “You prayed five rakaat.” Then he prostrated twice after the taslim. (Agreed upon).
3. If one forgets the first tashahud. The proof follows:
”The Prophet (pbuh) led them in Dhuhr one day and in the second rakaat he stood up after the second prostration (sajdah) without sitting. The people stood up with him. When the prayer was (almost) over and the people were awaiting the taslim, he said takbir (i.e., Allahu Akbar) while sitting, performed two prostrations before the taslim, and then made the taslim. ” (Bukhari)
Also mentioned in hadith is one who forgot to sit for the tashahud, then remembered before he stood all the way up. He should return to the sitting posture. However, if he is fully standing before he remembered, he should not sit back down, but at the end of the prayer, he should perform the sujud as-sahw.
4. If one is unsure how many rakaat he prayed. The Prophet (pbuh) said:
”If one of you becomes uncertain in his prayer, so that he does not know how many (units) he prayed – three rakaat or four, he should cast off what he is uncertain about, and build on what he is sure of, then perform two sajdah before making the taslim. So if he ends up praying five rakaat, his prayer will be taken as complete and if he prayed correctly it is like rubbing Satans nose in the dust. ” (Muslim).
5. If one is unsure about how many rakaat he prayed, he should go with the lower number. The Prophet (pbuh) said:
”If one of you is not sure between two and one, he should treat it as one (rakaat); and if he is not sure between two and three, he should treat it as two; and if he not sure between three and four, he should treat it as three. Because the uncertainty is in the extra rakaat. Then he should complete what is left of his salat and perform two sajdah while sitting before he makes taslim.” (Ahmed and others.)