Q: What is the ruling on shaking hands with the opposite gender?
A: It is recommended for men to shake the hands of other men and for women to shake the hands of other women. Shaking hands in such a manner when greeting is a means to expiate sin.
It is impermissible for a man to shake hands with a young lady. It is permissible, however, to shake hands with an elderly woman.
It was narrated (as a second narration) that Imam Ahmad disliked shaking the hands of women to the extent that he included that of the mahram (for whom there are details involved). He made exceptions for a father shaking the hand of his daughter(s), taking the hand of an elderly woman, or that of an unattractive woman.
Source: Kashshaf al-Qina', Ghayat al-Muntaha