It was narrated from Jabir that the Messenger of Allah (pbuh) said:

“If anyone of you has a bad dream, he should not tell people about how Satan played with him in his dream.”‏ ‏‏ Grade : Sahih (Darussalam) 1

The question you may find yourself asking is how can I control where my mind goes when I sleep?

You can’t.

But what you can do is model your nightly routine to follow the Sunnah and hadith of Prophet Muhammad (pbuh).

Al-Bara’ b. ‘Azib reported that Allah’s, Messenger (may peace be upon said:

When you go to bed, perform ablution as is done for prayer; then lie down on the right side and recite:

 

 وَاجْعَلْهُنَّ مِنْ آخِرِ كَلاَمِكَ فَإِنْ مُتَّ مِنْ لَيْلَتِكَ مُتَّ وَأَنْتَ عَلَى الْفِطْرَةِ

 

O Allah, I turn my face towards Thee and entrust my affair to Thee. I retreat unto Thee for protection with hope in Thee and fear of Thee. There is no resort and no deliverer (from hardship) but Thou only. I affirm my faith in Thine books which Thou revealed and in Thine Apostles whom Thou sent.”

Make this as the last word of yours (when you go to sleep) and in case you die during that night, you would die upon Fitra (upon Islam). And as I repeated these words in order to commit them to memory, I said:” I affirm my faith in Thy Messenger (Rasul) whom Thou sent.” He said: Say:” I affirm my faith in the Apostle (Nabi) whom Thou sent.” 2

It’s also important to recite Ayatul Kursi before bed:

‘By reciting it, there will be a guardian appointed over you from Allah who will protect you during the night, and Satan will not be able to come near you until morning.” 3

Narrated Abu Qatada: The Prophet (saws) said,

“A good dream that comes true is from Allah, and a bad dream is from Satan, so if anyone of you sees a bad dream, he should seek refuge with Allah from Satan and should spit on the left, for the bad dream will not harm him.” (Sahih al-Bukhari hadith # 6986) 4

Before going to bed Prophet Muhammad would also make sure he would be clean and would preform wudu. 5

If you wake up in the middle of a bad dream you can do the following:

1. Spit on your left three times. (Spit: A form of spitting comprising mainly of air with little spittle)
2. Seek refuge in Allah from shaytan and the evil of what you saw (Three times). 

أَعُوذُ بِاللهِ مِنَ الشَّيْطَانِ الرَّجِيمِ
a‛oodhu billaahi minash-shayṭaanir-rajeem

3. Do not relate it to anyone.
4. Turn and sleep on the opposite side to which you were sleeping on previously.
5. Get up and pray if you so desire.

References:
1. In-book reference : Book 35, Hadith 21 English translation : Vol. 5, Book 35, Hadith 3913. Sunan Ibn Majah Hadith         3913
2. Reference : Sahih Muslim 2710 a In-book reference : Book 48, Hadith 75 USC-MSA web (English) reference : Book      35, Hadith 6544
3. Sunnah.com reference : Book 9, Hadith 30 Arabic/English book reference : Book 9, Hadith
4. Reference : Sahih al-Bukhari 6986 In-book reference : Book 91, Hadith 5 USC-MSA web (English) reference :                Vol. 9, Book 87, Hadith 115
5. Reference : Sahih Muslim 2710 a In-book reference : Book 48, Hadith 75 USC-MSA web (English) reference : Book      35, Hadith 6544