Question: The one who reaches the Imam in the last Tashahud and he sees people enter the masjid. Does he enter with the Imam or pray with another congregation?
Answer: “What is apparent is if the person arrives and the people are in the original prayer praying, then he enters with them because he is one who caught the prayer and the congregation in general. And the second congregation is a difference of opinion and the congregation [going on] is the original congregation which the adhan was made for. So him entering is more deserving.
There is a difference of opinion on the second congregation: some say wait [for it], even though what is correct is a person can pray a second congregation as the Prophet (ﷺ) said:
أَلاَ رَجُلٌ يَتَصَدَّقُ عَلَى هَذَا فَيُصَلِّيَ مَعَهُ
Is there any man who may do good with this (man) and pray along with him.
[Saheeh Sunan Abi Dawood no. 574]
Some people of knowledge say there is no second congregation. However, what is correct is they pray. Unless he delays from coming from the first congregation [deliberately] knowing people will pray after him. That is not allowed”
[Sharh Ibn Majah no. 64]
Faisal Ibn Abdul Qaadir Ibn Hassan