Praise be to Allah.
If someone delays following his imam in one or more essential part of the prayer with a valid excuse, he should do what he lagged behind in, then follow his imam, and he does not have to do anything else.
Ibn Qudaamah (may Allah have mercy on him) said:
If the imam does an entire obligatory part of the prayer before the one who is praying behind him, such as if he bows and stands up again before the one who is praying behind him bows, due to an excuse such as drowsiness or overcrowding, or because the imam is going too fast, then that worshipper should do what the imam has already done, and catch up with his imam, and he does not have to do anything else. This was stated by Ahmad." (Al-Mughni (1/310).
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said:
Lagging behind the imam is of two types:
1.. Lagging behind due to an excuse.
2.. Lagging behind with no excuse.
In the first case, if it is due to an excuse, then the worshipper should do what he is lagged behind in doing, then follow the imam, and there is no blame on him, even if it involves one or two entire essential parts of the prayer. So if someone loses focus and becomes abent-minded, or he cannot hear his imam until the imam has gone ahead of him in one or two essential parts of the prayer, then he should do what he has lagged behind in, then follow his imam, unless the imam has reached the same point as he is at [in the following rak‘ah], in which case he should not do [what he has missed] and he should remain with the imam. In that case, one rak‘ah is valid on his part but two rak‘ahs are valid for his imam: the rak‘ah in which he lagged behind and the rak‘ah that the imam reached when he was at the same point of the rak‘ah.
If he realizes that he has lagged behind before the imam reaches the same point [in the following rak‘ah], then he should make up what he has missed then follow his imam. For example:
A man is standing [in prayer] with the imam, and the imam bows but the worshipper does not hear him bow, but when the imam says “Sami‘a Allahu liman hamidah,” the worshipper hears him say that. In this case we say to him: Bow and rise from bowing, then follow your imam, and you will have caught up with the rak‘ah, because in this case you lagged behind with an excuse.
In the second case, which is lagging behind with no excuse,
The worshipper will either lag behind his imam but catch up with him in the same essential part of the prayer, or he will lag behind in the entire essential part.
What is meant by lagging behind in the essential part of the prayer is that you lag behind in following him, but you catch up with the imam in the essential part of the prayer to which he has moved. For example, the imam bows when you still have one or two verses left of the soorah, and you remain standing until you complete your recitation, but you bow and catch up with the imam in bowing. In this case, the rak‘ah is valid, but the action is contrary to the Sunnah, because what is required is to start bowing when your imam reaches the posture of bowing, and not lag behind, because the Prophet (blessings and peace of Allah be upon him) said: “When he bows, then bow.”
So lagging behind in the entire essential part of the prayer means that the imam goes ahead of you in the movement of one essential part, meaning that he bows and stands up again before you bow.
The correct view is that if the worshipper lags behind the imam for an entire essential part of the prayer with no excuse, then his prayer is invalid, whether that essential part is bowing or anything else." (Ash-Sharh al-Mumti‘ 4/186-188).
But if the worshipper loses focus and fails to bow with his imam and only realizes when the imam is prostrating, so he prostrates with him without bowing, and if he fail to stand up after bowing with the imam, and prostrates with him from the bowing posture, that rak‘ah is rendered invalid, and he must do another rak‘ah after the imam says the tasleem and do the prostration of forgetfulness after saying the tasleem.
If he did not do a rak‘ah but he says the tasleem with the imam, and finishes his prayer, then that prayer of his is invalid, because he omitted one or more essential parts, so he must repeat it when he realizes.
And Allah knows best.