When a person has a bad or alarming dream, he is urged to take the following actions:
(1) Seek Allaah's سبحانه و تعالى protection from its evil.
(2) Seek Allaah's سبحانه و تعالى protection from Shaytaan.
(3) Lightly spit, three times, to the left side.
(4) Turn over and sleep on a different side.
(5) Say some of the evening and sleep adhkaar that he may have neglected or said without paying good attention - such as aayatul-kursee and the mu'awwidhaat.
(6) Get up and pray.
(7) Avoid narrating it.
(8) Avoid attempting to interpret it.
Al-Haafidh Ibn Hajar رحمه الله said: "Invoking Allaah's سبحانه و تعالى protection, turning over to the other side, and rising for prayer - these may help in warding off the harm that an unpleasant dream forebodes." 
And he also: "Whether an unpleasant dream is caused by Shaytaan or not, doing the recommended acts prevents any undesirable outcome. This is similar to what has been reported that supplication prevents affliction and charity protects against an evil end - and all of this happens according to Allaah's سبحانه و تعالى decree and measure." 
Let us discuss some of the recommended actions in further detail.
Ta'awwudh (or isti'aadhah) means seeking Allaah's سبحانه و تعالى protection or refuge. A believer is always required to ask Allaah سبحانه و تعالى for this, as Allaah سبحانه و تعالى says:
(Soorah Ghaafir, 40:56)
Ta'awwudh is particularly important when we feel the influence of Shaytaan's whispers. Allaah سبحانه و تعالى says:
(Soorah Fussilat, 41:36)
We have seen that some of our dreams are whispered by Shaytaan, and that we should say ta'awwudh when we see them. Ta'awwudh can be said simply as: (A'oothu billaahi minash-Shaytaanir-Rajeem - I seek Allaah's protection from Shaytaan, the outcast).
Al-Haafidh Ibn Hajar رحمه الله said: "As for seeking Allaah's سبحانه و تعالى protection from the dream's evil, it has an obvious reason, and it is recommended whenever a disliked matter occurs. And as for seeking Allaah's سبحانه و تعالى protection from Shaytaan, it is because some of the narrations indicate that it is caused by him." 
But in the case of dreams, it is better to say specific forms of ta'awwudh that have been reported in the Sunnah, because they protect, inshaa' Allaah, from Shaytaan, from the evil of the dream itself, and from other evils of the day and night.
Khaalid Ibn al-Waleed رضي الله عنه reported that he used to wake up terrified at night, so he would take his sword and strike anything he found in his way. He told the Prophet صلى الله عليه وسلم about this, and the Prophet صلى الله عليه وسلم instructed him: "Should I not teach you some words that the Trustworthy Spirit (Roohul-Ameen - Jibreel عليه السلام) taught me? Say: 'Aoothu bi-kalimaat-illaahit-taammaat, allaatee laa yujaawizuhunna barrun wa laa faajir, min sharri maa yanzilu minas-samaa'i wa maa ya'ruju feehaa, wa min sharri fitanil-layli wan-nahaar, wa min kulli taariq, illaa taariqun yatruqu bi-khayr, yaa Rahmaan - I seek protection with Allaah's complete words, which no righteous or corrupt person can violate, from the evil of whatever descends from heaven or ascends to it, and from the evil of the night's and day's tribulations, and from every night visitor - except for visitors that come with good - O Most Merciful (grant this)." 
'Abdullaah Ibn 'Amr رضي الله عنه reported that the Prophet صلى الله عليه وسلم said: "When one of you is terrified during his sleep, let him say: A'oothu bikalimaat-illaahit-taammah, min ghadabihi wa min sharri 'ibaadih, wa min hamazaat-ish-Shaytaani wa an yahduroon - I seek refuge in Allaah's complete words from His anger, from the evil of His creation, and from the spurring of the devils or that they be present with me." 
Imaam Ibraaheem an-Nakhaa'ee  رحمه الله proposed for a person who sees a hateful dream to say: "A'oothu bimaa 'aadhat bihi malaa'ikat-ullaahi wa rusuluh, min sharri ru'yaaya haadhihi, an yuseebanee feehaa maa akrahu fee deenee wa-dunyaaya - I seek (Allaah's) protection - in a way similar to that of Allaah's Angels and Messengers - from the evil of this dream, or that it might result in any harm to my religion or life." 
It is important to note that, in order to produce the required effect, ta'awwudh should be said with complete understanding and true belief in Allaah's سبحانه و تعالى power and His promised protection.
Al-Haafidh Ibn Hajar رحمه الله said: "Isti'aadhah should be said by sincerely turning to Allaah سبحانه و تعالى. It is not sufficient to merely move the tongue with it." 
Imaam al-Qurtubi رحمه الله said: "Isti'aadhah should be said for terrifying or distressing dreams, because they are insinuated by Shaytaan. When the dreamer says this - truly seeking Allaah's سبحانه و تعالى protection, and does the other acts of lightly spitting, turning over, and praying, Allaah سبحانه و تعالى will protect him from any harmful outcome of that dream."  
We have seen that it is recommended for a person who has had a bad dream to spit lightly to his left side. This is done to drive away Shaytaan and express despise and contempt of him, because he initiated the bad dream.
The words for this action used in various narrations range in meaning from mere "blowing" (nafth), to "blowing with light spit" (tafl or tafth), to "spitting" (basq),
The correct procedure is to say the ta'awwudh and then spit lightly to the left. This spitting (or blowing) is done by bringing the lips together, slightly sticking out the tip of the tongue from between them, and blowing with a humid breath - like spitting out a small seed.
In explanation of this, Al-Haafidh Ibn Hajar رحمه الله said: "Qaadee 'Iyaad رحمه الله said, 'Blowing is recommended for driving away the Shaytaan who attended the bad dream so as to humiliate and spite him. It is done on the left side because that is where dirty things are normally placed.'  And doing it three times is for emphasis." 
Ta'awwudh with light spitting constitutes a ruqyah. Ruqyah consists of words said in the form of du'aa' or dhikr for the purpose of protection or cure. It is sometimes accompanied by other actions, such as blowing or wiping over the thing to which it is applied.
Al-Haafidh Ibn Hajar رحمه الله said: "Al-Qaadee Abu Bakr Ibn al-'Arabi رحمه الله said, 'This (light spitting) is characteristic of ruqyah. (Doing it) would give confidence that (the dream's evil_ has been averted.' ... Qaadee 'iyaad رحمه الله said, 'As is common in a ruqyah, light spitting helps attain blessing from the humidity and air that are blown with the good words (of ta'awwudh).' ... And an-Nawawi رحمه الله said, 'Allaah سبحانه و تعالى makes this procedure a means for protection against the dream's evil - just as He makes charity a means for protecting wealth.'" 
A person who has had a bad dream is urged to turn over from the side on which he was sleeping to the other side. This helps refresh his body and circulate his blood, and allows him an opportunity to invoke Allaah سبحانه و تعالى and seek His protection.
It is even better to rise up from sleep and pray. Prayer fulfills the above benefits, in addition to countless other benefits. Al-Haafidh Ibn Hajar رحمه الله said: "Prayer (after having a bad dream) reflect submission to Allaah سبحانه و تعالى and seeking His shelter. By performing it, a person is protected from evil, and his request becomes true and complete, because of his nearness to His Lord during sujood (prostration).  As for turning over, it brings him home that his situation (fearing the bad dream) will also turn around." 
Imaam al-Qurtubi رحمه الله said: "Prayer fulfills all of these benefits: When a person rises to pray, he would surely change from the side on which he was sleeping. By performing wudoo', he would blow and spit to rinse his mouth and nose. He would say isti'aadhah prior to starting the recitation, and he would supplicate to Allaah while he is in the nearest situation to him. Thus, Allaah سبحانه و تعالى - through His kindness and generosity - would protect him from the dream's evil."