The above answer doesn't provide any of the proofs given by the one holding the opinion that it is prohibited for opposite genders to shake hands. Their proofs are linked in his answer to this answer in islamQA though, but not mentioned here.
I will answer from the standpoint that it generally is allowed to shake hands with opposite genders (while it is known that the majority of scholars maintain that it is unlawful).
Arguments proving it to be allowed
When we study fiqh, the basic understanding is that everything is allowed, except when it is proved otherwise. It is same to the context in court; "the burden of proof is on the one who declares, not on one who denies".
The proof that it is haram to shake hands is often derived from this hadith:
or one of you to be stabbed in the head with an iron needle is better for him than that he should touch a woman who is not permissible for him.
But according to this answer to the question: "What is meant by touch in this hadith?"
is that the hadith is clearly speaking about intercourse, and not just touch in its most literal sense.
Now, they also argue that the Prophet never shake hand with someone, therefore it is haram. The claim that the Prophet never shake hands might be correct, but that doesn't prove the conclusion to be correct. In fiqh, we do not say that everything the Prophet didn't do is haram.
`Aisha said, "And if any of the believing women accepted the condition (assigned in the above-mentioned Verse), Allah's Messenger (ﷺ) would say to her. "I have accepted your pledge of allegiance." "He would only say that, for, by Allah, his hand never touched, any lady during that pledge of allegiance. He did not receive their pledge except by saying, "I have accepted your pledge of allegiance for that."
Sahih Bukhari
Even though we have one hadith of Aicha saying that the Prophet never touched another womens hand, and even though the hadith of the Prophet saying to the women when they came to make bay'a: "I do not shake hands with women." Because he doesn't shake hands with women, whatever the context of the hadith is, doesn't prove it to generally be haram to shake hands with women.
"They said, 'Allah and His Messenger are more merciful to us than ourselves. Come, let us give our hands to you, Messenger of Allah!' The Messenger of Allah, may Allah bless him and grant him peace, said, 'I do not shake hands with women. My word to a hundred women is like my word to one woman.' " (sunnah.com - Muwatta Malik)
So these are the ahadith speaking about the Prophet not shaking hand. Logically we could end here and draw the conclusion saying that the ahadith doesn't prove it to be forbidden just because he didn't shake hands. For instance, its known that he didn't eat certain food, and that didn't mean it was forbidden.
There are some ahadith (1,2, 3), mentioning that some women even touched a sahabi's head in hajj:
then came to a woman of the tribe of Qais and she rid my head of the lice.
or
and went to a woman from my tribe who combed my hair or washed my head.
So, clearly a woman is touching his head.
In this and this video by Ali Gooma (Grand Mufti of Egypt, 2003-2013), he quotes some traditions that Umar Ibn Al Khattab shaked hand with the women when the prophet said that he isn't shaking hands with women. He also mentions that Abu Bakr shaked hand with a woman. He also mentions an old man getting foot massage by a woman
We also got some traditions where the prophet hold a womans hand (not shaking hand) from Sahih Al Bukhari:
Anas bin Malik said, "Any of the female slaves of Medina could take hold of the hand of Allah's Messenger (ﷺ) and take him wherever she wished."
and in this hadith it mentioned that the prophet didn't let go off the hand of the (slave) woman until she let go herself:
“If a female slave among the people of Al-Madinah were to take the hand of the Messenger of Allah (ﷺ), he would not take his hand away from hers until she had taken him wherever she wanted in Al-Madinah so that her needs may be met.”
Summarization
As said earlier, the basic in fiqh is that everything is allowed until proved otherwise. So according to the answer in islamqa, proof has been given that it isn't allowed. We will summarize
- The hadith, saying: "be stabbed in the head with an iron needle is better than touch a woman"
✓ It is talking about intercourse, therefore this proof is baseless.
- The Prophet said "I do not shake hands with women"
✓ This do not prove it to be haram, as explained earlier, just because the Prophet didn't do certain things, doesn't mean it is haram. (There are many examples of this in the Sunnah, see this for instance) We also have traditions showing that the Prophet did not let go of the (slave) women which hold his hand. We also have traditions of women combing men's (and the Prophets) hair, washing them and get rid of lice. This means the women touched the mans head at least., therefore these are bonus evidences which prove it isn't haram to shake hands. Because it would be enough to conclude that the hadith "I do not shake hands" is not proof enough.
Conclusion
Since we have argued against the proofs in the summarization above, we can conclude that there are no real (qat'i) proofs that it isn't allowed to shake hands with an opposite gender, therefore we cannot say it is haram. This is in accordance to our basic rule, that everything is halal until proven not.
So, it is generally allowed to shake hands with a non-mahram
Notice, if a person shakes hand with a non-mahram and somehow always gets aroused or something similar, then the ruling would change, but the basic rule (according to what we've concluded in this answer) it that it is allowed.
Answer to your question: In a situation like that, you just shake hands.
If you do follow the opinion that it isn't allowed to shake hands, many of those scholars actually count shaking hands of being within the small sins. If you are afraid that the situation might be awkward or somehow might lead to some fitna, i.e non muslim getting the wrong picture of muslims and islam, then the most wise would be to shake hands and do smile. Because we know: "Verily, the good deeds remove the evil deeds" and "al-fitnato ashaddo mina al-qatal" (And fitnah is greater than killing).
References:
Can I shake hands with non mahram women? - dar-alifta.org
علي جمعة - حكم مصافحة المراة - Ali Gomaa
حكم المصافحة بين النساء والرجال - Dar Al-Ifta Al-Masrriyah
لماذا تُحَرّمون مصافحة النساء؟
Disclaimer:
The majority of scholars maintain that it is unlawful.