Sayyiduna Abu Qatadah (may Allah be pleased with him) said: The Messenger of Allah (peace and blessings be upon him) said: "When you come for prayer, come calmly and with tranquility. Then whatever (part of the prayer) you catch, perform it, and whatever you miss, complete it afterwards."
Hadith Referenceسنن دارمي / من كتاب الصللاة / 1318
From these hadiths, it is understood that one should come to the prayer with calmness and tranquility. Even if there is a fear of missing the prayer, running is prohibited. Then, whatever portion of the prayer is attained with the imam should be performed, and after the imam completes the prayer with salam, the remainder of the prayer should be completed. In such a case, the rak‘ahs performed with the imam will be considered the first ones.