Praise be to Allah.
If a worshipper deliberately omitted the prostration of forgetfulness for omitting an obligatory part of the prayer, then his prayer is invalid, just as if he deliberately omitted any obligatory part of the prayer.
But if he forgot to do the prostration of forgetfulness, if he remembers it after saying the salaam he should do it, unless a long time has passed or he has left the mosque, in which case he does not have to do anything, and his prayer is valid.
Shaykh Ibn ‘Uthaymeen was asked about a man who forgot the first tashahhud, and he knew that he had to do the prostration of forgetfulness before the salaam, but he forgot and said the salaam. What is the ruling?
He replied: If he remembers within a short time, he should do the prostration, but if a long time has passed, then it is waived, such as if he did not remember until after a long time, if he had left the mosque then he does not have to go back to the mosque, and it is waived in his case. End quote.
Fataawa Ibn ‘Uthaymeen (14/50).
If the imam forgets to do the prostration of forgetfulness, the person praying behind him should not do it on his own. Rather he should say the salaam with the imam, then remind him after the salaam to do the prostration of forgetfulness, then the imam should prostrate and the person praying behind him should prostrate too.
And Allaah knows best.