Greeting others by saying “salam” and shaking hands are all signs of sociability and good manners and conduct. The prophet of Islam (pbuh) and the imams are at the pinnacle of sociability and good behavior and our role models, therefore we must follow them in this matter.
Anas ibn Malik narrates that when the prophet (pbuh) returned from the battle of Tabuk and Sa’d Ansari came to welcome him, he shook his hand.
The prophet of Islam has been narrated saying: “Whenever you meet each other, greet each other and shake hands, and ask Allah (swt) for each other’s forgiveness when parting.”
Shaking hands with the Muslim brother has a thousand rewards [according to narrations]. When the two who are shaking hands part, all of their sins have been forgiven [once again, according to narrations]. Shaking hands draws Allah’s (swt) mercy and blessings, is instrumental in making others happy, increases friendships, decreases dislike and hatred, changing them to friendship.
Shaking hands isn't something that solely belongs to the Shia, the Sunnis also acknowledge it because it is a tradition of the prophet (pbuh) and his excellency has emphasized on it both through his words and actions. Therefore it is of high importance to the Sunnis and they continue to practice it today.
It has been narrated that the first two individuals to start this act were Prophet Ibrahim (pbuh) and Dhul-Qarnayn; when the prophet came to welcome him upon his arrival, the two shook hands.
Yet, shaking hands with non-mahrams is haram and forbidden. After the conquer of Makkah, when the people wanted pledge their allegiance to the prophet of Islam (pbuh), he asked for a container, it was filled with water and he put his hand in it. He then ordered the women to put their hands in after him as a sign of them showing him their allegiance.
Regarding interacting with non-mahrams, it should not be in a way that will or even might lead to a forbidden act.
Your question can be broken down into four sections:
1- Why Muslims shake hands
2- It being a tradition of the prophet (pbuh)
3- It being a tradition amongst Sunni Muslims
4- Its position in other religions
Here we will discuss all of these points in detail.
Sociability and Shaking Hands
There is no doubt that Islam has invited all of us to be sociable with others, especially our brothers in faith, and to be kind with them. It is on this basis that in Islam, any act that causes a Muslim brother to become happy is commendable and admired (of course as long as the act isn't a forbidden one).
Saying “salam” and shaking hands signify sociability, that is why they are good and encouraged. The prophet of Islam (pbuh) and the imams are the highest in good conduct and sociability and all of us have been ordered to take them as role models, and they have taught us to do so [be sociable and of good conduct and morals] both through their words and actions.
Examples of the decent conduct of the infallibles
1- Anas ibn Malik narrates: “During the return of the prophet from the battle of Tabuk, Sa’d Ansari came to welcome him and the prophet (pbuh) shook hands with him…”.
2- Anas ibn Malik personally shook hands with the prophet (pbuh) and greeted him and the other narrators of hadith shook hands with the same hand and greeted him as well.
3- It has been narrated on behalf of Jabir in the book of Al-Imamah wal-Tabsirah that: “I came across the prophet (pbuh) and greeted him, his excellency [shook and] squeezed my hand saying: “Squeezing the hand of a brother [in faith] is like kissing him.”
4- Abi Ubaydah reports: “Me and Imam Baqir (as) were travelling on the same ride [camel or the like]; I would mount it first and he would climb on afterwards. Once he would get up, he would ask me how I was and would shake my hand, like two people who had met each other after being apart for a long time. When we wanted to get off, he would come down first and then me, and he would once again greet me and shake my hand, like one who hasn’t seen his friend for a while. “You are doing something no one has done before; one time is enough!”, I told him.
He replied: “You have no idea how much thawab [blessings] the shaking of the hands has. When brothers [in faith] see each other and shake hands, their sins continue to fall [and are forgiven] like how the leaves of trees fall, and Allah (swt) pays attention to [and bestows His grace upon] them until they part.
It has been narrated that the prophet (pbuh) said: “Greet each other whenever you meet and shake hands, and when it comes time to part, ask Allah (swt) for each other’s forgiveness.”
Also, it has been narrated from Imam Baqir (as) on behalf of the messenger (pbuh) of Allah (swt) that he said: “Whenever one of you meets his Muslim brother, he has to greet him and shake his hand, for Allah (swt) endowed the angels with this act, so you too practice what the angels do.”
The effects and blessings of shaking hands
a) Its effects in the hereafter:
1- Imam Sadiq has been reported saying: “Shaking hands with the mu’min has a thousand good deeds.”
2- It has also been narrated that he said shaking hands has a thousand times of rewards.”
3- Imam Baqir (as) has said: “When the mu’min shakes the hand of a fellow mu’min, the two part with all of their sins being forgiven.”
4- It has been stated in Thawabul-A’maal that Imam Sadiq (as) said: “When you shake hands, you are being rewarded the same reward as the mujahid [who fights in the way of Allah (swt)].”
b) The effects of shaking hands in this world:
1- Draws Allah’s (swt) grace and mercy
2- Makes others happy
3- Increases friendships
4- Causes bitterness and dislike to disappear
5- Changes animosity to friendship
Imam Sadiq (as): “Greeting those present [at a place] by shaking hands makes the greeting complete [instead of plainly greeting with words], and greeting the travelers [when they arrive] by embracing and doing mu’anaqah with them [which is a form of embracing in which the two embracing each other stick one side of their necks together and then the other side as well] makes the greeting complete.”
Shaking hands with the enemy
Imam Ali (as) has said: “Shake hands with your enemy even if he doesn’t like it; because Allah (swt) has ordered such a matter in the Quran: “Good and evil [conduct] are not equal. Repel [evil] with what is best. [If you do so,] behold, he between whom and you was enmity, will be as though he were a sympathetic friend.” He went on to say: “You aren’t capable of defeating your enemy so it is better for you to listen to Allah (swt) and forgive him in order to obtain great blessings; there is nothing better for you than to see your enemy in the state of disobeying Allah (swt) [because Allah’s (swt) disobedience brings punishment].” [Note that not all enemies might be meant here; what is probably meant here is another Muslim whom you are having enmity with, so the hadith isn't telling us to be good with the enemies of Islam who in no way cease harming Islam and the Muslims.]
Shaking hands amongst the Sunni Muslims
Shaking hands isn't something that only belongs to the Shia; it is a tradition of the prophet (pbuh). His excellency has emphasized on this matter both through his actions and sayings. That is why it is important to our Sunni brothers as well and is practiced amongst them today.
Sahih Tirmidi narrates on his behalf from Anas ibn Malikthat whenever anyone would meet with the prophet (pbuh) and shake his hand, his excellency wouldn’t let go of the person’s hand until he himself would end the handshake.
The history of handshaking
There are many traditions practiced amongst Muslims today that are actually Abrahamic traditions. One of those is handshaking. In his book of Ilalul-Sharaye’, Saduq narrates from Imam Baqir (as) that he said: “The first two people to shake hands were Prophet Ibrahim (pbuh) and Dhul-Qarnayn; when the prophet went to welcome Dhul-Qarnayn [upon his arrival], the two shook hands.”
Shaking hands with non-mahrams
One must pay attention that shaking hands with non-mahrams has been excluded from this tradition and prohibited, that is why Islamic scholars have issued fatwas on its impermissibility.
Aban ibn Taghlib reports on behalf of Imam Sadiq (as) that he said: “After the conquer of Makkah, its men pledged allegiance to the prophet (pbuh). After them, the women came to do the same; it was at this moment that the verse “یا أَیُّهَا النَّبِیُّ إِذا جاءَکَ الْمُؤْمِناتُ یُبایِعْنَکَ، ...” was revealed. The daughter of Hareth asked the prophet (pbuh) how the women were to pledge their allegiance to him, his excellency answered: “I don’t [can't] shake hands with women.” So he asked for a container full of water and put his hand in the water and pulled it out and then ordered the women to put their hands in it afterwards as a form of showing their allegiance.
Mufaddal says: “I asked Imam Sadiq (as) how the women of the prophet’s (pbuh) time pledged their allegiance to him [if it was by shaking hands or something else]. He answered: “He asked for a container that was used for wudhu, it was filled with water and he put his hand in it and asked them to do the same; this was how it was done.”
What is for certain is that we have to interact with non-mahrams in a way that won't lead to a forbidden act. We even have to refrain from interacting in a way that might lead to such a thing.
As for coming into physical contact with non-mahrams, grand Islamic scholars have issued fatwas on its absolute impermissibility, unless there is a barrier [such as clothing] that doesn’t allow direct contact between the two, or unless there are other important reasons that oblige one to do so [like when there is no choice but for a non-mahram doctor to make physical contact with a non-mahram patient and his/her treatment depends on it and there are no other mahram doctors who can do the job]. 
This ruling [that no physical contact is permissible between non-mahrams] applies to all non-mahrams, regardless of how close the two might be; even family aren’t an exception. Therefore cousins and in-laws all have to observe this ruling as well no matter how close a relationship they might have.
 “Indeed the Messenger of Allah is an Outstanding Exemplar for those who Have hope in Allah's Grace and in The Last Day and who keep on Saying Allah's Remembrance” Ahzab:21.
 Al-Hadith (hadiths on training) vol.1, pg.217.
 Mohammad Baqir Majlisi, Biharul-Anwar, vol.16, pg.17.
 Musa Khosravi, Zendeganiye Hazrate Sajjad va Imam Mohammade Baqir, pg.213; Iman va Kufre Biharul-Anvar,vol.2, pg.309.
 Sheikh Hasan Deylami, Irshadul-Qulub ilal-Sawab, vol.1, pg.392.
 Khorramshahi and Ansari, Payame Payambar, pg. 427.
 Seyyed Hidayatullah Seyyed Mustarhimi, Irshadul-Qulub, vol.2, pg.162.
 Ibid, pg.392.
 Saduq, Khisal, vol.1, pg.73.
 Muhammad Baqir Majlisi, Biharul-Anwar, vol.16, pg. 18.
 Muhammad Baqir Behboudi, Gozideye Kafi, vol.1, pg.355.
. Azizullah Atarodi, Iman va Kufre Biharul-Anwar, vol.2, pg.309.
 Sahih Tirmidi, vol.2, pg.80
 Seyyed Morteza Firouzabadi, Faza’ele Panj Tan alayhimussalam dar Sihahe Sheshganeye Ahle Sunnat, vol.1, pg. 214.
 Fatemeh Mashayekh, Qisasul-Anbiya’ (Qesase Quran), pg.230.
 Azizullah Atarodi, Iman va Kufr, vol.1, pp.104-105.
 Tawzihul-Masa’ele Maraje’, vol.2, issue 2442; Ibid, pg.809, second question; Masa’ele Jadid, vol.1, pp.137-138.
 Imam Khomeini, Nejatul-Ibad, pg.364, issues 23 and 24.
 With help from Index no.527 (website: ).