Assalamualaikum ustaz. What is the ruling of shaking hands with a person of the opposite gender? If a person of the opposite gender has already held out his hands to shake hands, is it permissible for us to shake his hands to not offend him? Hope for an explanation.
Waalaikumussalam wrt. wbt.,
Alhamdulillah, praise and gratitude to Allah SWT for His countless blessings for us all. Praise and salutations to our beloved Prophet Muhammad PBUH, his family, companions, and all those who follow his footsteps until the Final day.
Islam is a religion of mercy and calls for mankind to strengthen their bonds of brotherhood. Islam greatly encourages its ummah to spread salam and shake hands with one another.
Shaking hands to maintain silaturrahim is an honour and those who do so are promised forgiveness for their sins. This is as stated in a hadith narrated by al-Bara’ bin Azib, where the Rasullullah PBUH said:
مَا مِنْ مُسْلِمَيْنِ يَلْتَقِيَانِ فَيَتَصَافَحَانِ إِلاَّ غُفِرَ لَهُمَا قَبْلَ أَنْ يَفْتَرِقَا
“Two Muslims will not meet and shake hands having their sins forgiven them before they separate.” 
Hence, according to this hadith, it is sunnah for a man to shake hands with another man. Likewise, it is also sunnah for a woman to shake hands with another woman. 
In another hadith, it is narrated from Qatadah RA he said to Anas bin Malik RA:
أَكَانَتِ المُصَافَحَةُ فِي أَصْحَابِ النَّبِيِّ صَلَّى اللهُ عَلَيْهِ وَسَلَّمَ؟ قَالَ: نَعَمْ
“Was it a custom of the companions of the Prophet (ﷺ) to shake hands with one another?” He said, “Yes.” 
Al-Hafiz Ibn Hajar cited that Ibn Batthal said, shaking hands is a good deed according to most scholars. Imam al-Nawawi also said that shaking hands is a sunnah that is agreed upon when meeting someone. 
This act is also acknowledged by jurists who said that shaking hands is sunnah according to the total number of sahih and hasan evidences. 
Shaking hands with the opposite gender
Regarding the issue of shaking hands with the opposite gender, there is a hadith from Muhammad bin Munkadir RA, he said, I heard from Umaimah binti Ruqaiqah who said:
جِئْتُ النَّبِيَّ صلى الله عليه وسلم فِي نِسْوَةٍ نُبَايِعُهُ فَقَالَ لَنَا: فِيمَا اسْتَطَعْتُنَّ وَأَطَقْتُنَّ إِنِّي لاَ أُصَافِحُ النِّسَاءَ
“I came to the Prophet (ﷺ) with some other women, to offer our pledge to him. He said to us: ‘(I accept your pledge) with regard to what you are able to do. But I do not shake hands with women.’” 
Likewise, in a hadith from Saidatina A’ishah R.Anha, she said:
وَاللَّهِ مَا مَسَّتْ يَدُهُ يَدَ امْرَأَةٍ قَطُّ فِي الْمُبَايَعَةِ، مَا يُبَايِعُهُنَّ إِلاَّ بِقَوْلِهِ
“By Allah, his hand never touched, any lady during that pledge of allegiance. He did not receive their pledge except by saying,” 
Imam Ibn Hajar al-Haitami in his commentary prohibits man and woman from shaking hands:
فَلَا يُصَافِحُ الرَّجُلُ الْمَرْأَةَ وَلَا عَكْسُهُ
“It is prohibited for a man from shaking hands with a woman and vice versa.” 
In this matter, Imam Bujairimi in his Hasyiah said: “And it is sunnah to shake hands with the same gender and for opposite gender with a mahram relationship, husband and wife, children that aren’t attractive yet (has yet to induce desire) or old people with a cloth in between their hands, then it is permissible without any desires or it doesn’t lead to fitnah.” 
The opinion of Syeikh Yusuf al-Qardawi in his fatwa states:
جواز مصافحة الرجل للمرأة بشرطين هما أن تكون هناك ضرورة، وحال أمنت الفتنة
“It is permissible to shake hands between man and woman in an exigent situation and safe from fitnah.” 
Syeikh Ali Juma’h said: “For the ruling of shaking hands between an ajnabi man and woman. Scholars have differing opinions on the matter. Jumhur scholars prohibit it except madhhab Hanbali and Hanafi who permit shaking hands with the elderly if it doesn’t induce desires nor will it lead to fitnah.” 
Coming back to the above question, scholars have differing opinions regarding the matter of shaking hands with people of the opposite gender. And jumhur scholars including scholars of madhhab Syafi’e prohibit shaking hands between a man and woman who isn’t mahram.
Although there is an opinion which permits it on a condition, it is best to avoid a khilaf matter and this is commanded.
We are inclined to celebrate the local madhhab and situation. For example, in Malaysia. Most of us are madhhab Syafi’e. However, if in certain circumstances and situations, it is permissible on the set conditions.
 Narrated by Abu Daud (5212) dan al-Tirmizi (2727). Imam al-Tirmizi evaluated this hadith as hasan gharib.
 See Hasyiah al-Baijuri ‘ala Ibn al-Qasim, 1/178
 Narrated by al-Bukhari (6263)
 See Fath al-Bari, 11/55
 See al-Mausu’ah al-Fiqhiyyah al-Kuwaitiyyah, 37/356
 Narrated by Ibnu Majah (2984)
 Narrated by Imam Bukhari (4891)
 See Tuhfah al-Muhtaj fi Syarh al-Minhaj, 3/56
 See Hasyiah al-Bujairimi ala al-Khatib, 3/385
 See Dar al-Ifta al-Misriyyah, fatwa no. 2287