An-Nawawi says: “It is permissible to engage in lawful conversation in the mosque and one may discuss worldly affairs and other things and even laugh, as long as it is about something permissible. This opinion is based on the hadith of Jabir ibn Samurah who said: ‘The Prophet would not rise from his place of the morning prayer until the sun had risen, and when the sun rose, he would get up. And they would talk and laugh about [pre-Islamic] days of ignorance, and he would smile.'” This is related by Muslim.
When one wants to leave the mosque he should step with his left foot first and say: “In the name of Allah! O Allah, shower blessings upon Muhammad. O Allah, forgive my sins for me and open for me the doors of Your bounty. O Allah, protect me from the accursed Satan.”