Assalamualaikum ustaz. When I arrived at the mosque, the obligatory prayer has already started. Is it permissible for me to first perform the tahiyatul masjid prayer, then follow the other congregants to perform the obligatory prayer to get the rewards of tahiyatul masjid prayer?
Alhamdulillah, praise and gratitude to Allah SWT for His countless blessings for us all. Praise and salutations to our beloved Prophet Muhammad PBUH, his family, companions and all those who follow his footsteps until the Last Day.
The Meaning of Sunnah Tahiyatul Masjid Prayer
Sunnah Tahiyatul Masjid prayer is a two rakaat prayer by everyone who enters a mosque before he sits. This is based on a hadith from Abu Qatadah al-Sulami RA, where the Messenger PBUH said:
إِذَا دَخَلَ أَحَدُكُمْ الْمَسْجِدَ فَلْيَرْكَعْ رَكْعَتَيْنِ قَبْلَ أَنْ يَجْلِسَ
“When one of you enters the mosque, he should pray two rak’as before sitting down.” 
Imam al-San’ani in explaining this hadith said the hadith contains the restriction against sitting when a person enters a mosque before performing a two rakaat prayer. 
Imam al-Nawawi stated that the scholars have agreed that the tahiyatul masjid prayer is sunnah and if there is nothing that prevents a person from performing it, then it is makruh to sit before performing this two rakaat prayer. 
Is It Permissible to Perform the Tahiyatul Masjid Prayer When the Obligatory Prayer Has Already Started
In the above issue, when the imam has started to perform the obligatory prayer, then it is impermissible for us to perform any sunnah prayer. This is in accordance with a hadith from Abu Hurairah RA, where the Prophet PBUH said:
إِذَا أُقِيمَتْ الصَّلاَةُ فَلاَ صَلاَةَ إِلاَّ الْمَكْتُوبَةُ
“When the time for prayer comes, only the prescribed prayer is to be said.” 
Since when a makmum walks into a mosque the imam is already praying, then he has to immediately pray together with the imam.
Th author of the book al-Fiqh al-Manhaji stated: Tahiyyatul Masjid is automatically considered to happen when a person performs the obligatory prayer or other sunnah prayers. Its purpose is so that a person doesn’t sit inside a mosque before he first prays. 
Hence, the obligatory prayer performed is sufficient without performing the tahiyatul prayer. The deference is the same.
Furthermore, the Tahiyatul Masjid prayer is also included in a type of prayer that can be combined with other specific prayers such as the obligatory prayer and sunnah Subuh prayer.
In the book al-Mausu‘ah al-Fiqhiyyah, it is explained: “If a person combines two worship in the intention, then if both of them can be combined such as sunnah Friday bath and the junub bath, or junub bath with the obligatory bath due to the ending of menstruation, or sunnah Friday bath and sunnah Eid bath; even concurrently when one of the two worships isn’t specific such as sunnah Tahiyyatul Masjid prayer combined with obligatory prayer or other sunnah prayers, Then all of these doesn’t affect the worship for the meaning of purification is that it can be combined with one another and Tahiyyatul Masjid and others like it isn’t meant with the specification of the prayer, for what it really means is to enliven the place to perform the prayer, then it can also be combined with other prayers (with the prayer).”
Here, we state this is included in the issue of al-indiraj or in other words, combining two worships such as sunnah Tahiyyatul Masjid with obligatory prayer itself has represented the sunnah Tahiyyatul Masjid prayer.
May Allah gives us understanding in fulfilling all His commandments. Amin.