If a man joins a gathering it is Sunnah for him to give salams to people in this gathering, and when he wants to get up and leave, he should give them salam once again before he leaves. That is because of the report narrated by Abu Dawood (5208) and al-Tirmidhi (2706), and classed as hasan by al-Tirmidhi; and also narrated by Ahmad (7793) from Abu Hurayrah (may Allah be pleased with him) who said: The Messenger of Allah (blessings and peace of Allah be upon him) said: “When one of you comes to a gathering, let him say salam, and when he wants to leave let him say salam, for the former is not more of a duty than the latter.” Classed as saheeh by al-Albani in Saheeh Abi Dawood and elsewhere.
Al-Mubarakfoori (may Allah have mercy on him) said:
Al-Teebi said: i.e., just as the first salam was telling them that they are safe from his evil when he was present, the second salam is telling them that they are safe from his evil when he is absent. Giving salam when joining the gathering is not more important than giving salam when leaving the gathering; rather the second is more important. End quote.
Tuhfat al-Ahwadhi (2/402-403)
Al-Nawawi (may Allah have mercy on him) said:
The apparent meaning of this hadeeth is that it is obligatory for the group to return the salam of the one who gave them salam and left them. Imam al-Qadi Husayn and his companion Abu Sa‘d al-Mutawalli said: The custom among some people is to say salam when leaving a gathering, and that is a du`a (supplication) to which it is mustahabb to respond but is not obligatory, because the greeting is only given when meeting and not when parting. This is what they (these two scholars) said, but it was criticised by Imam Abu Bakr al-Shashi, the last of our companions, who said: This is wrong, because giving salam is Sunnah when leaving just as it is a Sunnah when joining the gathering. This hadeeth proves that. And what al-Shashi said is the correct view. End quote.
Al-Adhkar, p. 258
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said:
In this hadeeth we see that when a man enters upon a gathering, he should say salam. And when he wants to leave and he gets up to leave the gathering, he should also say salam, because the Prophet (blessings and peace of Allah be upon him) enjoined that and said: “for the former is not more of a duty than the latter.”, i.e., just as when you say salam when you enter, you should also say salam when you leave. Hence when a person enters the mosque, he sends salams upon the Prophet (blessings and peace of Allah be upon him), and when he leaves he also sends blessings upon him. And when he enters Makkah for ‘Umrah or Hajj, he should start with tawaf, and when he leaves Makkah he should end with tawaf, because tawaf is the greeting for Makkah for the one who enters it for Hajj or ‘Umrah, and it is bidding farewell to Makkah for the one who has done Hajj or ‘Umrah and is now leaving. This is a sign of the perfection of sharee‘ah, because it makes the beginning and the end the same in such matters as these. End quote.
Sharh Riyadh al-Saliheen, p. 990
And Allah knows best.