Giving salaam is Sunnah when joining a gathering and when leaving it
If a man joins a gathering it is Sunnah for him to give salaams to people in this gathering, and when he wants to get up and leave, he should give them salaam 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 salaam, and when he wants to leave let him say salaam, for the former is not more of a duty than the latter.” Classed as saheeh by al-Albaani in Saheeh Abi Dawood and elsewhere.
Al-Mubaarakfoori (may Allah have mercy on him) said:
Al-Teebi said: i.e., just as the first salaam was telling them that they are safe from his evil when he was present, the second salaam is telling them that they are safe from his evil when he is absent. Giving salaam when joining the gathering is not more important than giving salaam 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 salaam of the one who gave them salaam and left them. Imam al-Qaadi Husayn and his companion Abu Sa‘d al-Mutawalli said: The custom among some people is to say salaam when leaving a gathering, and that is a du‘aa’ (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-Shaashi, the last of our companions, who said: This is wrong, because giving salaam is Sunnah when leaving just as it is a Sunnah when joining the gathering. This hadeeth proves that. And what al-Shaashi said is the correct view. End quote.
Al-Adhkaar, 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 salaam. And when he wants to leave and he gets up to leave the gathering, he should also say salaam, 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 salaam when you enter, you should also say salaam when you leave. Hence when a person enters the mosque, he sends salaams 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 tawaaf, and when he leaves Makkah he should end with tawaaf, because tawaaf 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 Riyaadh al-Saaliheen, p. 990
And Allah knows best.