All perfect praise be to Allah, The Lord of The Worlds, and may His peace and blessings be upon our Prophet Mohammad and upon all His family and companions.
There is a set of rules governing the Imam and the persons praying behind him. If the Imam was inside the Masjid and the persons praying behind him were outside it then there should be no barrier-e.g. a wall-between the rows inside the Masjid and the ones outside it. It is also conditioned that the distance between the Imam and the persons praying behind him doesn`t exceed one hundred and fifty meters, as stated in the Shafie book {Al-Eqna`, 1/168}: "If the Imam prays inside the Masjid and those following him pray outside although they are close to it (Masjid), provided that the distance is no more than three hundred cubits from the end of the Masjid, then it is permissible, because the Masjid is one entity."
If there are rows outside the Masjid, although there are spaces in the rows inside, then that doesn`t invalidate the congregational prayer, but its reward is decreased, as stated by the prominent scholar Ash-Shirbini: "It is from Sunnah that all spaces are filled and all rows become full before a new row is started, and this is recommended. However, if the persons praying behind the Imam failed to do so then their prayer is valid, but disliked." {Moghni Al-Mohtajj, 1/493}.
In conclusion, in order for the prayer of those following the Imam from outside the Masjid to be valid, it is conditioned that the distance is short and there is no barrier between the rows outside and inside the Masjid. Moreover, the prayer of the ones who didn`t fill the spaces of the first rows is valid, but disliked. And Allah knows best.