Abu Hurayrah quoted the Prophet (sallAllahu ‘alayhi wasallam) as saying:
“The prayer of a man in congregation is twenty-five times more superior (in reward) to his prayer in his house or market – and this is because he performs ablution and perfects it and goes to the mosque with the sole purpose of performing the prayer. He does not take a step without being raised a degree and having one of his sins erased. When he prays, as long as he does not lose his ablution, the angels keep on praying [for him] ‘O Allaah, bless him. O Allaah, have mercy upon him.’ And he is considered in prayer as long as he is waiting for the prayer.” (Reported by Al-Bukhari and Muslim)
In general, if a person stays in the masjid to remember Allaah (dhikr) and worship Him, or waiting for the prayer, all of these are good deeds and acts of worship that bring one closer to Allaah.