The traveller has to offer the prayer in full if he prays behind an imam who is offering it in full
When the traveller prays behind a non-traveller, he has to offer the prayer in full, whether he catches up with the entire prayer or just one rak’ah, or less. Al-Athram said: I asked Abu ‘Abd-Allaah (i.e., Imam Ahmad) about the traveller: should he join in the tashahhud of residents? He said: He should pray four rak’ahs. That was narrated from Ibn ‘Umar, Ibn ‘Abbaas and a number of the Taabi’een, and was also the view of al-Shaafa’i and Abu Haneefah.
The evidence for that is:
1 – The words of the Prophet (peace and blessings of Allaah be upon him): “The imam is appointed to be followed, so do not differ from him.” Narrated by al-Bukhaari, 722; Muslim, 4141.
2 – The report narrated by Ahmad from Ibn ‘Abbaas, that it was said to him: “Why does the traveller pray two rak’ahs when he is alone and four when he follows an imam who is a resident (non-traveller)?” He said: “That is the Sunnah” – meaning that it is the Sunnah of the Messenger of Allaah (peace and blessings of Allaah be upon him). Classed as saheeh by al-Albaani in Irwa’ al-Ghaleel, 571.
3 – This is what ‘Abd-Allaah ibn ‘Umar (may Allaah be pleased with him) did. Naafi’ said: When Ibn ‘Umar prayed with the imam he would pray four rak’ahs and when he prayed on his own he would pray two. Narrated by Muslim.
And Shaykh Ibn ‘Uthaymeen said:
If the traveller prays with an imam then he has to offer the prayer in full, because of the general meaning of the words of the Prophet (peace and blessings of Allaah be upon him): “The imam is appointed to be followed.” And because the Sahaabah (may Allaah be pleased with them) used to pray behind the caliph ‘Uthmaan ibn ‘Affaan during Hajj in Mina, and he used to lead them in praying four rak’ahs so they offered four rak’ahs with him.
Similarly if the traveller joins the prayer when the imam is offering the last two rak’ahs, after the imam says the salaam he has to stand up and complete his prayer, making it four rak’ahs, because of the general meaning of the words of the Prophet (peace and blessings of Allaah be upon him): “Whatever you catch up with, pray, what whatever you miss, complete it.” Narrated by al-Bukhaari, 635; Muslim, 603. In this case the person’s prayer is connected to the imam, so he has to follow the imam even with regard to whatever he missed of the prayer.
If a person used to pray two rak’ahs behind a non-travelling imam in the past, then he has to repeat the four-rak’ah prayers in which he did that. He does not have to offer them all in one go. He should try to work out the number of prayers involved and repeat them.
Liqa’ al-Baab al-Maftooh, p. 40-41.