When to Offer Sujud As-Sahw If You Are Following an Imam
Question

What is the ruling on following the Imam with regard to the prostration of forgetfulness? What is the ruling on doing the prostration of forgetfulness if you are praying behind an Imam?
Praise be to Allah.When to offer Sujud As-sahw if you are following an Imam and he forgot something in prayer
The one who is praying behind an Imam must follow the Imam in doing Sujud As-Sahw (the prostration of forgetfulness) if he prayed all the Rak`ahs with him, i.e., if he did not join the prayer late. That is because of the general meaning of the words of the Prophet (peace and blessings of Allah be upon him): “The Imam is only appointed to be followed, so do not differ from him. When he bows, then bow, when he says ‘Sami’a Allahu liman hamidah (Allah hears the one who praises Him)’ then say ‘Rabbana laka’l-hamd (Our Lord, to You be praise)’, and when he prostrates, then prostrate.” (Narrated by Al-Bukhaari, 722 and Muslim, 414) 
With regard to the one who joins the prayer late and has missed one Rak`ah or more, he should follow the Imam if he does the prostration of forgetfulness before the Salam, but he should not follow him if he does it after the Salam because the one who joins the prayer late cannot say the Salam with the Imam, rather he has to make up what he has missed, then say the Salam, then do the prostration of forgetfulness , then say the (final) Salam. 
This is in general terms. As for a more detailed discussion, we can sum up the issue of the person praying behind an Imam and the prostration of forgetfulness as follows: 

If a person offers the entire prayer with the Imam, then the Imam makes a mistake and does the prostration of forgetfulness, he has to follow him, whether he does the prostration before the Salam or after.  
If a person joins the prayer late, and the Imam makes a mistake in the part of the prayer that he catches up with, then it depends: if the Imam does the prostration of forgetfulness before the Salam, the person praying behind him should prostrate with him, then complete his prayer, then do the prostration of forgetfulness once again, because the first prostration that he did with the Imam was not at the proper place for him, because the prostration of forgetfulness cannot be done during the prayer, rather it should be done at the end of the prayer, so his prostration with the Imam was merely for the purpose of following the Imam. 

If the Imam prostrates after the Salam, then the person who joined the prayer late should not prostrate with him, rather he should stand up and complete his prayer and say the Salam, then do the prostration of forgetfulness and say the Salam. 

If the person praying behind the Imam joined the prayer late, and the Imam made a mistake in the part of the prayer that he did not catch up with, such as if he made a mistake in the first Rak`ah and the person joined the prayer in the second Rak`ah: 

If the Imam prostrates before the Salam, the person praying behind him should follow him, then complete his prayer, but he does not have to prostrate again because the ruling on his Imam’s mistake does not apply to him. 
If the Imam does the prostration after the Salam, the person praying behind him should not follow him, and he does not have to prostrate after finishing his prayer either, because the ruling on his Imam’s mistake does not apply to him, as the mistake happened before he joined the Imam in the prayer. 
How to offer Sujud As-Sahw if you forgot something behind an Imam
All of these cases apply when the mistake is made by the Imam. If the mistake is made by the person praying behind the Imam, that is also subject to further discussion: 

If the person praying behind the Imam makes a mistake in his prayer, and he did not join the prayer late, i.e. he caught up with all the Rak`ahs with the Imam, such as if he forgets to say “Subhaana Rabbiy Al-`Athim (Glory be to my Lord the Most Great)” when bowing, then he does not have to do the prostration of forgetfulness, because the Imam carries it for him. But if we assume that the person made a mistake that invalidates one of the Rak`ahs, such as if he forgot to recite Al-Fatihah, then in that case he must stand up when the Imam says the Salam and repeat the Rak`ah that was invalidated because of his mistake, then he should recite the Tashahhud, say the Salam and do the prostration of forgetfulness after the Salam. 

If the person praying behind the Imam makes a mistake in his prayer, and he joined the prayer late, then he should do the prostration of forgetfulness whether he made the mistake whilst he was praying with the Imam or after he stood up to complete the part of the prayer that he missed, because if he prostrates he will not be differing from the Imam, as the Imam will already have finished his prayer. (See: Risalah fi Ahkam Sujud As-Sahw by Shaykh Ibn ‘Uthaymin (may Allah have mercy on him) 
And Allah knows best.