How to Offer Condolences in Islam
Question

What is the Islamic way to offer condolences? What is the ruling on gatherings for mourning?
Praise be to Allah.Significance of offering condolences 
Offering condolences means comforting the bereaved, urging them to show patience by reminding them of the reward, and praying for the deceased and the one affected by calamity. This is how the jurists (may Allah have mercy on them) have defined it, including Ibn Muflih in Al-Furu` (2/229).
Undoubtedly offering condolences is something that makes it easier for the bereaved to bear his loss, and it helps to relieve grief and distress. Hence Islamic teachings recommend offering condolences to the one who has been stricken by calamity, so as to achieve the objective of cooperating in righteousness and piety, showing patience and accepting the divine will and decree, and urging one another to adhere to the truth and to show patience.
Hence, the Prophet (blessings and peace of Allah be upon him) would offer condolences to his companions when they were stricken by calamity, and the Muslims have continued to offer condolences to one another and comfort one another. The scholars unanimously agreed that it is prescribed and recommended to offer condolences.
An-Nawawi (may Allah have mercy on him) said: 
“You should understand that offering condolences is encouraging people to show patience, and saying that which will comfort the bereaved lessen his grief and mitigate the calamity that has befallen him. It is something that is recommended, because it includes enjoining what is right and forbidding what is wrong. It is also included in the verse in which Allah, may He be Exalted, says (interpretation of the meaning): {And cooperate in righteousness and piety} [Al-Ma’idah 5:2]. This is the best evidence that can be quoted with regard to offering condolences. And it is proven in an authentic narration the Messenger of Allah (blessings and peace of Allah be upon him) said: “Allah will help a person so long as he helps his brother.” (Al-Adhkar  p. 148-149)
What to say when offering condolences 
Condolences may be offered in any words that bring comfort to the affected person and encourage him to show patience and seek reward with Allah. 
Ash-Shawkani (may Allah have mercy on him) said: 
“Anything that may be said to the affected person and encourage him to show patience is regarded as offering condolences, no matter what wording is used, and the one who offers condolences will attain thereby the reward that is mentioned in the Hadiths." (Nayl Al-Awtar,  4/117)
Among the phrases of condolences that were narrated from the Prophet (blessings and peace of Allah be upon him) are the words: “To Allah belongs what He has taken, to Him belongs what He has given, and everything has an appointed time with Him, so be patient and seek reward.”
An-Nawawi (may Allah have mercy on him) said:
“The best words that may be said when offering condolences are those that were narrated in Al-Bukhari and Muslim from Usamah ibn Zayd (may Allah be pleased with him) who said: One of the daughters of the Prophet (blessings and peace of Allah be upon him) sent word to him, calling him to come and telling him that a son of hers was dying. He said to the messenger: “Go back to her and tell her that to Allah, may He be Exalted, belongs what He has taken, to Him belongs what He has given, and everything has an appointed time with Him. And tell her to show patience and seek reward with Allah…”
I [An-Nawawi] say: This Hadith is one of the greatest principles in Islam, as it includes many of the fundamental principles of the faith and its minor issues, as well as etiquette and showing patience in the face of all calamities, worries, sickness and other troubles. What is meant by the words “To Allah, may He be exalted, belongs what He has taken” is that the entire universe belongs to Allah, may He be Exalted. So He has not taken what belongs to you; rather He has taken what belongs to Him, which was like a loan to you. What is meant by the words “and to Him belongs what He has given” is that whatever He gives you has not gone out of His possession; rather it still belongs to Him, may He be Glorified, and He may do with it whatever He wills. “And everything has an appointed time with Him,” so do not panic, because the time for the one He has taken away has reached its appointed end, so it could not be delayed or brought forward from that time. Once you have understood all of this, then show patience and seek reward with Allah for what has befallen you." (Al-Adhkar,  p. 150).
Place and manner of offering condolences 
With regard to the place for offering condolences and the manner in which that is to be done, there is no specific ruling. That may be done by meeting in the mosque or in the street or at work , or by speaking on the phone, or by sending various kinds of messages, or by going to the person’s house, or by doing anything that people will customarily regard as offering condolences . 
Time for offering condolences
The time for offering condolences begins from the moment the deceased passes away, and it is recommended to offer condolences both before and after the burial. It is not limited to only three days .
Shaykh Ibn Baz (may Allah have mercy on him) said:
“There is no specific time for offering condolences, and no particular days. Rather it is prescribed from the time of the burial onwards. Hastening to offer condolences is better in the case of an extreme calamity, and it is permissible to offer condolences after three days of the death of the deceased, because there is no evidence to suggest that there is any time limit." (Fatawa Islamiyyah,  2/43)
In Fatawa Al-Lajnah Ad-Da’imah (9/134) it says: “There is no specific time for offering condolences, and no specific place.”
And Allah knows best.