When you become aroused, your brain sends chemical messages to the blood vessels in the penis, causing them to dilate or open, allowing blood to flow into the penis. As the pressure builds, the blood becomes trapped in the corpora cavernosa, keeping the penis erect. If blood flow to the penis is insufficient or if it fails to stay inside the penis, it can lead to erectile dysfunction.
The causes of erectile dysfunction include aging, high blood pressure, diabetes mellitus, cigarette smoking, atherosclerosis (hardening of the arteries), depression, nerve or spinal cord damage, medication side effects, alcoholism or other substance (drug) abuse, pelvic surgery including radical prostatectomy, pelvic radiation, penile/perineal/pelvic trauma such as pelvic fracture, Peyronie's disease (a disorder that causes curvature of the penis and sometimes painful erections), and low testosterone levels.
The first line and by far the most common treatment today is with the prescription drug sildenafil citrate, sold under the brand name Viagra. An estimated 20 million prescriptions for the pill have been filled since it was approved by the FDA in March 1998. It is also the most effective treatment with a success rate of more than 60%. The drug boosts levels of a substance called cyclic GMP, which is responsible for widening the blood vessels of the penis. In clinical studies, Viagra produced headaches in 16% of men who took it, and other side effects included flushing, indigestion, and stuffy nose.
One report from a recent community survey concluded that erectile failure was the leading complaint of males attending sex therapy clinics. Other studies have shown that erectile disorders are the primary concern of sex therapy patients in treatment. This is consistent with the view that erectile dysfunction may be associated with depression, loss of self-esteem, poor self-image, increased anxiety or tension with one's sexual partner, and/or fear and anxiety associated with contracting sexually transmitted diseases, including AIDS.
The Cancer of the Prostate Strategic Urologic Research Endeavor (CaPSURE) study, designed to determine whether an individual man’s sexual outcomes after most common treatments for early-stage prostate cancer could be accurately predicted on the basis of baseline characteristics and treatment plans, found that 2 years after treatment, 177 (35%) of 511 men who underwent prostatectomy reported the ability to attain functional erections suitable for intercourse. 
Is your erectile dysfunction due to psychological (stress, relationship problems, etc.) or physical factors? Your doctor may ask if you note erections at night or in the early morning. Men have involuntary erections in the early morning and during REM sleep (a stage in the sleep cycle with rapid eye movements). Men with psychogenic erectile dysfunction (erectile dysfunction due to psychological factors such as stress and anxiety rather than physical factors) usually maintain these involuntary erections. Men with physical causes of erectile dysfunction (for example, atherosclerosis, smoking, and diabetes) usually do not have these involuntary erections. Men with psychogenic erectile dysfunction may relate the onset of problems to a "stressor," such as failed relationship. Your doctor may suggest a test to determine if you have erections during sleep, which may suggest that there may be a psychological cause of the erectile dysfunction.
For the patient whose history suggests organic impotence, further history, physical and laboratory data will help identify the cause. The classification listed in Table 187.2 is based on the pathophysiologic scheme presented above, and includes mechanical problems that can interfere with erection. Vascular disease is the most common cause of impotence. In advanced cases, Lehriche's syndrome of aortoiliac occlusion will be suggested by bilateral thigh or calf claudication, loss of muscle mass in the buttocks and legs, and impotence. However, the majority of patients with vascular impotence have less severe vascular disease and many will have occlusive disease of the hypogastric-cavernous bed only. Even among patients without claudication, vascular disease is still a likely cause of impotence, especially if risk factors for atherosclerosis are present. Nonatherosclerotic disease is a consideration in the patient with a history of trauma or radiation to the pelvis, both of which cause fibrosis of vessels.
Just as certain meds can make it difficult for men to have an orgasm, some can keep the flagpole from even getting raised in the first place. Anti-depressant medications like Prozac and Zoloft, anti-anxiety pills like Valium, high blood pressure medicine like Diuril, and even over-the-counter cold medicines like Sudafed and anti-heartburn pills like Zantac can inhibit erections.
Having erection trouble from time to time isn't necessarily a cause for concern. If erectile dysfunction is an ongoing issue, however, it can cause stress, affect your self-confidence and contribute to relationship problems. Problems getting or keeping an erection can also be a sign of an underlying health condition that needs treatment and a risk factor for heart disease.