It is permissible for a child who has reached the age of discernment, which is the age of seven years, to stand in the row with adults during the congregational prayer. This is indicated by the report narrated by al-Bukhaari (685) from Anas (may Allah be pleased with him) who said: I and an orphan in our house prayed behind the Prophet (blessings and peace of Allah be upon him), and my mother Umm Sulaym (prayed) behind us.
The author of ‘Awn al-Ma‘bood Sharh Sunan Abi Dawood (2/264) said: If there is one boy, he should stand in the row with the men, and should not stand on his own behind the row. This was the view of as-Subki, and is supported by the hadeeth of Anas. The orphan did not stand on his own; rather he stood in the row with Anas (may Allah be pleased with him). End quote.
Shaykh ‘Abdullah al-Bassaam (may Allah have mercy on him) said: The majority are of the view that it is valid for a boy to stand in the row in both obligatory and supererogatory (naafil) prayers. They quoted as evidence for that the saheeh hadeeth, because Anas described his companion as an orphan.
The well-known view among the Hanbalis is that it is valid for him to stand in the row in a supererogatory (naafil) prayer, on the basis of this hadeeth, but it is not valid for him to stand in the row in an obligatory prayer. But we have stated previously that the rulings that apply to one of the types of prayer [obligatory or supererogatory] are also applicable to the other type, because their rulings are the same. Anyone who singles out one type of prayer for a ruling has to provide evidence, and there is no evidence in this case.
Therefore the correct view is that of the majority. This view was favoured by Ibn ‘Aqeel among the Hanbalis, and was regarded as correct by Ibn Rajab in al-Qawaa‘id.
Based on that, it may be understood from the hadeeth that it is valid for one who has not yet reached puberty to stand in the row for the prayer, because the word orphan (yateem) refers to someone whose father has died and he has not reached the age of puberty.
End quote from Tayseer al-‘Allaam Sharh ‘Umdat al-Ahkaam (1/106)
It says in Fataawa al-Lajnah ad-Daa’imah (vol. 2, 6/337): Boys may accompany their fathers or their mothers to the mosque, if there is fear for them [if they are left at home]; there is nothing wrong with that, because that was done at the time of the Prophet (blessings and peace of Allah be upon him). But they must be kept under control and not allowed to play about in the mosque and disturb other worshippers. Those who have reached the age of seven or older should be told to do wudoo’ and pray, so that they may get used to doing that and so that they and their parents may earn reward. There is nothing wrong with making them stand in the row, and doing so does not create gaps in the row as the questioner suggested, because their prayer is valid and because the children used to stand in the rows with the adults behind the Prophet (blessings and peace of Allah be upon him). End quote.
And Allah knows best.