What the latecomer who joins the prayer with the imam catches up with is the first part of his prayer
The correct scholarly view is that whatever the latecomer who joins the prayer with the imam catches up with is the first part of his prayer. This is the view of al-Shaafa’i (may Allaah have mercy on him), see al-Majmoo’ by al-Nawawi, 4/420. The Prophet (peace and blessings of Allaah be upon him) said: “If you hear the iqaamah then walk to the prayer, and you must be calm and dignified, and do not hasten. Whatever you catch up with, pray, and whatever you miss, make it up.” (al-Bukhaari, 636; Muslim, 602). This is what is meant by whatever the latecomer who joins the prayer with the imam catches up with being the first part of his prayer.
On this basis, if the latecomer joins the imam in the second rak’ah of Maghrib prayer, then this rak’ah is the second for the imam and the first for this person who is praying behind him, then the third rak’ah for the imam is the second rak’ah for the person praying behind him. Then when the imam says the salaam, the person behind him should stand up and complete his prayer, and this will be the third rak’ah for him, and he should recite al-Faatihah silently in it. And Allaah knows best.
Sheikh Muhammed Salih Al-Munajjid