Technically speaking, there are three possible causes for erectile dysfunction.
1) Psychological (Gumdrop Girl did a great job of describing how that happens in some cases, above) -- it can also happen because a guy is afraid it's going to happen, or because he has performance anxiety, or any of a bunch of reasons.
2) Neurological -- there may be damage to nerves in the penis and surrounding tissue. This sometimes happens due to injury or illness.
3) Circulatory -- there can be damage to blood vessels that are responsible for erection and maintenance of erection. This also can happen due to injury or illness, and sometimes due to side effects from medications given for things like high blood pressure or heart disease.
Obviously, a guy who has had problems with erectile dysfunction in the past is likely to think about it whenever he's getting ready to have sex. This means that psychological and physiological reasons for erectile dysfunction can combine. It's a pretty complicated issue, but a combination of working on attitudes about sex and sexual performance, patience, and (where it's warranted) medical intervention for circulatory or neurological problems can usually take care of a recurrent erectile dysfunction problem.
However, most guys (somewhere around 80%) WILL have at least one episode of erectile dysfunction some time in their lives. It can be kind of a wildcat thing, and can happen for psychological reasons of which the man isn't necessarily even aware on the surface. Usually, unless there are chronic psychological or physical problems involved, it happens once or twice, then goes away again on its own.
In any event, it's nothing to be ashamed of -- our bodies just don't always do what we want them to. There are a lot of other ways to have sexual pleasure and a lot of other ways to please a partner if one's penis isn't cooperating on any given occasion. Taking the pressure off, and getting involved in doing something else that is fun and sexual, often solves the problem all by itself.
Start a Revolution -- Stop Hating Your Body!