All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad is His slave and Messenger. We ask Allaah to exalt his mention as well as that of his family and all his companions.
No doubt, what the questioner has mentioned about some people who hit children and throw them out of the mosques is totally contrary to Islamic principles. Going to mosques while accompanying children was a common practice during the period of the Prophet ; much evidence supports this fact. For example, Imaam Ahmad reported from Abu Bakrah that he said: "Sometimes the Prophet used to lead people in prayer and Al-Hasan Ibn Ali both, would ride on his back whenever the Prophet prostrated. This happened more than once." Imaam Ahmad also reported that Abu Hurayrah said: "We were performing I'shaa' prayer with the Prophet and whenever he prostrated Al-Hasan and Al-Husayn both, would sit on his back. Whenever the Prophet raised his head from the prostration he removed them gently from his back and put them on the ground. He continued dong so until he finished the prayer. Thereafter, he put them in his lap. Abu Hurayrah said, 'I asked the Prophet 'should I take them to their mother? He replied "No". Suddenly, there was lightening, then the Prophet said to them: "Go to your mother"; they went to their mother seeing their way with the light of lightening."
Imaam An-Nasaa'ee reported in a long Hadeeth that Al-Hassan jumped on the back of the Prophet during one of the Maghrib or I'shaa' prayers. The Prophet made a long prostration and after finishing the prayer, he said: "My child rode upon me, so I disliked to rush him until he fulfilled his joy."
The Prophet also said: "When I stand for the prayer, I intend to prolong it, but on hearing the cries of a child, I cut it short, as I dislike to trouble the child's mother." [Al-Bukhari]
There are many other narrations in this matter.
As for the narration: "Keep your children away from your mosques", this is not an authentic narration. Therefore, this narration should not be an excuse to beat the children or expel them out of the mosque or to be harsh with them.
On the contrary, there are many benefits in taking children to the mosques; for instance, it makes them habituated to the prayers and to attend study circles in the mosques, etc. Moreover, taking them to the mosques is a useful means of protecting them from bad friends and from the wide-spread amusement and entertainment that badly affect them.
On the other hand, we would like to make clear that when we allow taking children to the mosques, we do not permit them to disturb people, or to play with the Qur'aan, or to damage the belongings of the mosque, or even soil the mosque, etc. So, the children's guardians have to supervise them and teach them the etiquettes of the mosque.
Finally, we inform the questioner that mistakes of the Muslims or some praying person should not be attributed to Islam. In fact, Islam must not be measured by the mistakes of its followers, but rather all should be judged by Islam.
Allaah knows best.