Discouraging women from going to mosques is based in ahadith:
لا تمنعوا نساءكم المساجد وبيوتهن خير لهن
Do not prevent your women from visiting the mosque; but their houses are better for them
— Abu Dawud
قد علمت أنك تحبين الصلاة معي، وصلاتك في بيتك خير من صلاتك في حجرتك، وصلاتك في حجرتك خير من صلاتك في دارك، وصلاتك في دارك خير من صلاتك في مسجد قومك، وصلاتك في مسجد قومك خير من صلاتك في مسجدي
I know that you love to pray with me, but praying in your house is better for you than praying in your courtyard, and praying in your courtyard is better for you than praying in the mosque of your people, and praying in the mosque of your people is better for you than praying in my mosque.
— Saheeh ibn Hibban and Saheeh Ibn Khuzaimah
And it was also discouraged by the Sahaba.
Additionally, the fundamental teaching in Islam on this matter is that women are prescribed to stay at home unless there is some necessity, as taught in the Quran and the Hadith etc. Going to the mosque is not a necessity as it is neither obligatory nor recommended for women in pray in the mosques.
Further, as Aisha said:
لو أن رسول الله صلى الله عليه وسلم رأى ما أحدث النساء لمنعهن المسجد كما منعت نساء بني إسرائيل
If the Messenger of Allah ﷺ had seen the way the women are behaving, he would have forbidden them to go to the mosque as the women of the Children of Israel were forbidden.
— Bukhari and Muslim
Which highlights the basic principle of Shariah that fitnah must be avoided, and that this principle exists in our law as well as the previous laws. It is because of this principle that the scholars considered it disallowed or discouraged for women to go even to mosques - as moral corruption has worsened than what was present at the time of the Prophet ﷺ and even at the time of Aisha.
أجمعوا على أنه لا يرخص للشواب منهن الخروج في الجمعة والعيدين وشيء من الصلاة؛ لقوله تعالى {وقرن في بيوتكن} [الأحزاب: ٣٣] والأمر بالقرار نهي عن الانتقال ولأن خروجهن سبب الفتنة بلا شك، والفتنة حرام، وما أدى إلى الحرام فهو حرام. وأما العجائز فلا خلاف في أنه يرخص لهن الخروج في الفجر والمغرب والعشاء والعيدين، واختلفوا في الظهر والعصر والجمعة قال أبو حنيفة: لا يرخص لهن في ذلك وقال أبو يوسف ومحمد يرخص لهن في ذلك.
There is consensus that young women will not be allowed to go out for Jumu’ah, Eid or any other prayer, for Allah has said: {And abide in your houses} [al-Ahzab 33:33] and the command to stay is a prohibition of going out, and because their going out is without any doubt a cause of fitnah, and fitnah is haram, and whatever invites to haram is itself haram.
As for old women there is no difference that they are permitted to go out for Fajr, Maghrib, Isha and Eid prayers, And there is difference on Zuhr, Asr and Jumu’ah. Abu Hanifa said: They are not be allowed. And Abu Yusuf and Muhammad allowed it (the allowance for the night time prayers is because darkness acts as concealment)
— Badaa'i' as-Sanaa'i'
ويكره لهن حضور الجماعات يعني الشواب منهن لما فيه من خوف الفتنة ولا بأس للعجوز أن تخرج في الفجر والمغرب والعشاء وهذا عند أبي حنيفة رحمه الله " وقالا يخرجن في الصلوات كلها لأنه لا فتنة لقلة الرغبة إليها فلا يكره كما في العيد وله أن فرط الشبق حامل فتقع الفتنة غير أن الفساق انتشارهم في الظهر والعصر والجمعة أما في الفجر والعشاء فهم نائمون وفي المغرب بالطعام مشغولون والجبانة متسعة فيمكنها الاعتزال عن الرجال فلا يكره
It is makruh for women to attend congregational prayers - meaning the young women, due to fear of fitnah.
There is nothing wrong if old women go out for Fajr, Maghrib and Isha - which is held by Abu Hanifa - and it is said by the two jurists that old women may go out for all prayers - because there is no fitnah due to the lack of attraction towards them. Hence it is not makruh just like Eid prayers.
(Abu Hanifa's argument is) that men can be attracted towards old women and that would invite fitnah. However at the time of Zuhr and Asr the impious men roam around. While at the time of Fajr and Isha they are asleep. And at the time of Maghrib they are busy in eating. And comparison with Eid prayers is not correct because Eid is offered outside the city in an open space where it is possible for women to be far from men - hence that is not makruh.
— Hidayah sharḥ Bidayat al-Mubtadi