The Gemara comments that when somebody has a frightening dream, and is concerned that it might portend some misfortune, observing a fast has the ability to prevent any possible effects of the dream. In fact, the Gemara goes so far as to say that fasting eliminates the effects of a dream "like fire burns straw."
The Shulhan Aruch (Orah Haim 288) rules that even if one has a bad dream on Friday night, and he is very concerned and worried about what it might mean, he may fast the next day, on Shabbat. Although fasting is generally forbidden on Shabbat, in this case it is permitted. However, the Shulhan Aruch adds that since this individual failed to fulfill the Misva of Oneg Shabbat (enjoying oneself on Shabbat), he must observe a second fast to atone for neglecting this Misva. Even though the fast was allowed due to his anxiety over his dream, nevertheless, the fact remains that he failed to fulfill the obligation of Oneg Shabbat, and this requires atonement through an additional fast. This second fast may be observed on Monday, and does not have to be observed on Sunday.
As an aside, this Halacha demonstrates the great importance of the Misva of Oneg Shabbat. Even when depriving oneself of enjoyment on Shabbat is allowed, it still requires atonement, due to its unique importance. Unfortunately, there are many people who do not conduct festive Shabbat meals as Halacha requires, either because they prefer staying in bed, or because they simply prefer not to bother. The Halacha concerning Ta’anit Halom (fasting after a frightening dream) underscores for us the importance of feasting on Shabbat and the gravity our Sages afforded to the neglect of this Misva. Even though the person in this case "enjoyed" fasting in that it provided him some comfort and peace of mind after his frightening dream, it nevertheless requires atonement. And thus even if one prefers staying in bed, and says he finds that more enjoyable, he may nevertheless not neglect the Misva of the Shabbat meals.
Regarding the general subject of Ta’anit Halom, the Shulhan Aruch cites an opinion that the concept of fasting after a frightening dream applies only if one had the same frightening dream three times; after the third time, he may observe a fast. There is also an opinion that this concept applies only if a person had one of the following three dreams: his teeth fell out, a Sefer Torah or Tefillin was burning, or he dreamt of himself on Yom Kippur. Others add to this list a dream that one got married, that one was reading a Sefer Torah, and that the beams of one’s house were falling.
Regardless, Hacham Ovadia Yosef, in a responsum in Yehaveh Da’at, writes that nowadays, one should not pay too much attention to dreams or be concerned about them. Frightening dreams could be the result of eating a large meal, thoughts that one had during the day, or an attempt by the evil inclination to confuse a person. Therefore, one should not be overly concerned about a frightening dream. Hacham Ovadia writes that when he would be approached by somebody who was frightened by a dream, he would advise the person to read Tehillim, spend some extra time learning Torah, give charity, or observe a Ta’anit Dibur (a day when he refrains from all speech other than prayer and study).
Rav Eliezer Papo (1785-1828), in his Hesed La’alafim (288; listen to audio recording for precise citation), makes an important comment on this topic that is relevant to life generally, and not just to dreams. He writes that sometimes a person can, Heaven forbid, bring misfortune upon himself through excessive worry and anxiety. If a person is frightened and scared of something, this itself can, occasionally, cause the unfortunate event to happen. Accordingly, the Hesed Alafim writes if a person approaches a Rabbi for guidance on Shabbat morning after being frightened by a dream on Friday night, the Rabbi should not advise him to fast. If the person hears the Rabbi tell him he must fast on Shabbat because of the dream, this will exacerbate his anxiety, as he will think this is a very urgent and dire situation. The heightened anxiety could then cause harm to befall him. Instead, the Rabbi should encourage him, assuring him that dreams are not a cause for concern, and advising him to repent, read Tehillim and undertake other measures of spiritual growth.
In conclusion, then, in our times it is far preferable not to take dreams seriously, and if a person is frightened, he should work towards strengthening his connection to God through repentance, Torah study, prayer and so on. In an extreme case where a person cannot overcome his anxiety over a dream, he can fast, even on Shabbat, but as a general rule, this should not be done.
Summary: If a person has a frightening dream, he should not be concerned. If he is worried, he should undertake measures such as reading Tehillim, studying Torah, giving charity and the like. In an extreme case, where one is beset by worry and fear because of a dream, he may observe a fast, even on Shabbat, but if he fasts on Shabbat he must observe an additional fast to atone for having deprived himself of enjoyment on Shabbat.