Assalamualaikum ustaz. What is the ruling of talking and asking how another person is doing in a mosque? Hope for an explanation. Thank you.
Waalaikumussalam wrt. wbt.,
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 Final day.
We begin with the statement of Allah SWT:
فِي بُيُوتٍ أَذِنَ اللَّـهُ أَن تُرْفَعَ وَيُذْكَرَ فِيهَا اسْمُهُ يُسَبِّحُ لَهُ فِيهَا بِالْغُدُوِّ وَالْآصَالِ
“[Such niches are] in mosques which Allah has ordered to be raised and that His name be mentioned therein; exalting Him within them in the morning and the evenings.” 
Ibn Kathir stated that Allah SWT commanded us to honour the mosques by purifying them from any filth, useless matters, word or words that aren’t related to it. This is also the opinion of Ibn Abbas, ‘Ikrimah, al-Dhahhak and many more among the commentary scholars. 
Talking in a mosque
Regarding this matter, al-Nawawi stated that it is permissible to talk on mubah matters  in a mosque, whether it is of worldly matters or others, even if it is accompanied by laughter and others as long as it is still included in permissible matters. 
There is a hadith from Jabir bin Samurah RA, that said:
كَانَ لاَ يَقُومُ مِنْ مُصَلاَّهُ الَّذِى يُصَلِّى فِيهِ الصُّبْحَ حَتَّى تَطْلُعَ الشَّمْسُ فَإِذَا طَلَعَتْ قَامَ وَكَانُوا يَتَحَدَّثُونَ فَيَأْخُذُونَ فِى أَمْرِ الْجَاهِلِيَّةِ فَيَضْحَكُونَ وَيَتَبَسَّمُ
“He did not stand up (and go) from the place where he offered the dawn prayer until the sun rose, and after the rising of the sun he stood up, and they (his Companions) entered into conversation with one another and they talked of the things (that they did during the Days of Ignorance), and they laughed (on their unreasonable and ridiculous acts). Allah’s Messenger (ﷺ) smiled only.” 
The above hadith shows that it is permissible to talk about matters in the time of Jahiliyyah and other matters of the ummah. Furthermore, it is also permissible to laugh. However, it is better to only smile as displayed by the Prophet PBUH. Moreover, scholars said that it is makruh to laugh frequently and among the scholars, it is poor conduct. 
Furthermore, Ibn Qudamah also said that it is makruh to be heedless in speech related to worldly matters.  The same is stated by former Saudi Arabia Grand Mufti, Syeikh ‘Abd al-‘Aziz when he was asked regarding the ruling of a person who talks while waiting to pray regarding matters other than zikrullah or the words of the Prophet PBUH in a mosque.
He answered, that the mosque is not a place to discuss worldly matters, however, it is a place to remember Allah (zikrullah), recite the Quran, pray, worship, class or discussion about knowledge and others like it. Their act – that is to talk about worldly matters –is makruh, for they should actually busy themselves with beneficial matters in the hereafter such as reciting the Quran, reciting dhikr, discuss beneficial matters in the hereafter and others. This is what a believer should do.
If they only talk briefly regarding worldly matters, then this is fine, for instance, a person talks to another about what is going to happen related to worldly affairs. However, if a specific ceremony is held to discuss worldly matters – or it is talked excessively – then it is makruh. 
Thus, talking in a mosque is permissible as long as it doesn’t disturb others who are worshipping in the mosque. It should be reminded that the speech must be included as permissible matters that aren’t excessive nor is it prohibited matters such as backbiting, insults and setting someone against each other and others. This includes useless and unbeneficial matters for it is nearer to heedlessness. There is a hadith from Anas bin Malik RA, Rasullullah PBUH said:
يَأْتِي عَلَى النَّاسِ زَمَانٌ يَتَحَلَّقُونَ فِي مَسَاجِدِهِمْ وَلَيْسَ هِمَّتُهُمْ إِلاَّ الدُّنْيَا لَيْسَ لِلَّهِ فِيهِمْ حَاجَةٌ فَلاَ تُجَالِسُوهُمْ
“There will come a time when people will gather in a mosque and they intended nothing (at the time) except the worldly matters. Allah doesn’t need what they do and don’t you sit with them.” 
I’d also like to take this opportunity to mention a widely spread hadith in our society, it is said the Prophet PBUH said:
مَنْ تَكَلَّمَ بِكَلامِ الدُّنْيَا فِي الْمَسَاجِدِ ، أَوْ فِي الْمَسْجِدِ ، أَحْبَطَ اللَّهُ تَعَالَى أَعْمَالَهُ أَرْبَعِينَ سَنَةً
“Whoever talks about worldly matters in mosques or a mosque, then Allah SWT will invalidate his deeds for 40 years.”
This is a fake hadith as stated by al-Shaghani. Likewise, al-Qari also said this hadith is bathil in terms of mabna (the matan structure) and meaning.  Hence, we state that this narration or hadith is fake and we are prohibited from claiming that it is from the Prophet PBUH or spreading it to others without explaining that it is fake.
According to the above discussion and arguments, in our opinion that it is permissible to talk in a mosque as long as it doesn’t disturb others who are worshipping in the mosque. Concurrently, he should be mindful of the manners in a mosque. Furthermore, the matters talked about must be permissible and not excessive matters and not prohibited or unbeneficial matters.
Although we would also like to advise, if possible, it is best to avoid useless talk or decrease talking regarding worldly matters in a mosque, hence it is best for a believer to busy himself with beneficial deeds that will help them in the hereafter such as reciting the Quran, dhikr and discussions on beneficial matters for them in the hereafter.
May Allah SWT grant us all a clear understanding in this religion. Amin.
 Surah al-Nur: 36
 See Tafsir Ibn Kathir, 6/62.
 Matters that are neither prohibited nor obligated in Islam. In other words, permissible matters. (See https://prpm.dbp.gov.my/cari1?keyword=mubah.)
 See al-Majmu’ Syarh al-Muhazzab, 2/180.
 Narrated by Muslim (6179)
 See al-Minhaj Syarh Sahih Muslim, 15/79.
 See al-Mughni, 1/329.
 Narrated by al-Hakim dalam al-Mustadrak (7916)
 See al-Asrar al-Marfu‘ah, pg. 338; al-Kasyf al-Khafa’, 2/285.