Speaking to non-mahram women may occur because of a need or it may occur needlessly.
If it is done needlessly and only for fun and enjoyment, then there is no doubt that it is haraam and comes under the heading of the zina of the tongue and ears of which the Prophet (blessings and peace of Allah be upon him) spoke when he said:
“The son of Adam’s share of zina has been decreed for him, which he will inevitably get. The zina of the eyes is looking, the zina of the ears is listening, the zina of the tongue is speaking, the zina of the hands is touching, and the zina of the foot is walking. The heart longs and wishes, and the private part confirms that or denies it.”
Narrated by Muslim, 2657.
When there is a need to speak to a woman, the basic principle is that it is permissible, but it is essential to pay attention to the following etiquette:
The conversation should be limited to only what is necessary and has to do with the matter at hand, without talking too much or branching off into other topics. Think about the etiquette of the Sahaabah (may Allah be pleased with them) and compare it with the way things are today. The Mother of the Believers ‘Aa’ishah (may Allah be pleased with her) narrated the story of the slander (al-ifk) that the hypocrites accused her of; in her hadeeth she (may Allah be pleased with her) said:
Safwaan ibn al-Mu‘attal al-Sulami al-Dhakwaani was behind the army and had set out at the end of night. In the morning he reached the place where I was and he saw the shape of a person sleeping. He recognized me when he saw me, as he used to see me before the hijab was enjoined. I woke up when I heard him saying Inna Lillaahi wa inna ilayhi raaji’oon (Verily to Allah we belong and verily unto Him is our return) when he recognized me, and I covered my face with my jilbab. By Allah, we did not exchange a word and I did not hear any word from him apart from his saying Inna Lillaahi… He made his camel kneel down and put his foot on its foreleg (to keep it steady), then I mounted it, and he set off, leading me on the mount, until we came to the army.
Narrated by al-Bukhaari, 4141 and Muslim, 2770.
Al-‘Iraaqi (may Allah have mercy on him) said: The phrase “and I did not hear any word from him” is not repeating the previous idea (“we did not exchange a word”). It is possible that he did not speak to her; rather he spoke to himself or he recited Qur’aan out loud or said dhikr out loud such that it could be heard. But none of that happened. He did not speak to her; rather he used silence in that situation out of good manners and politeness, and because of the seriousness of the situation in which he found himself.
This hadeeth also shows good manners with non-mahram women, especially in the case of being alone with them out of necessity in the wilderness or elsewhere, as Safwaan did when he made his camel kneel without speaking or asking questions.
Tarh at-Tathreeb, 8/53
Avoiding joking and laughing; that is not part of etiquette and dignity.
Avoiding staring and always trying hard to lower the gaze as much as possible; if there is a quick glance for the purpose of speaking, there is nothing wrong with that, in sha Allah.
Not softening the voice, by either party, or choosing soft words; rather they should speak is the same, ordinary tone of voice as they would speak to anyone else. Allah, may He be exalted, says, addressing the Mothers of the Believers (interpretation of the meaning): “then be not soft in speech, lest he in whose heart is a disease (of hypocrisy, or evil desire for adultery, etc.) should be moved with desire, but speak in an honourable manner” [al-Ahzaab 33:32].
Avoiding the use of any words that may have some suggestive meanings, and so on.
Not going to extremes in embellishing one’s speech . Some people use their skills in communication with others by movements of the hand or face or by quoting poetry or proverbs or romantic phrases. This is a means that the Shaytaan uses to open the door to haraam attraction between the sexes.
Ibn al-Qayyim (may Allah have mercy on him) said:
None of the poets see anything wrong with talking to, addressing or looking at non-mahram women, but this is contrary to Islam and common sense, and it is exposing oneself to temptation. How many people have been affected in this way with regard to their religious commitment and worldly affairs.
And Allah knows best.
(Source - Islam Q&A)