You can have nightmares for all kinds of reasons, including anxiety, sleep deprivation, drug and alcohol use, and medical conditions like obstructive sleep apnea and post-traumatic stress disorder (PTSD). Nightmares can also be a side effect of certain medications. But why? That part is less clear. More @Wikipedia
Hover over any link to get a description of the article. Please note that search keywords are sometimes hidden within the full article and don't appear in the description or title.