Now that Thomasperraz has explained very well, the physiology of the penis and its erection, let us see what are the various factors that cause Erectile Dysfunction.
A sensory stimulation due to touch or sight or a mental stimulation due to a thought, or both, cause the brain to send an impulse through the spinal cord, and then through the concerned local nerves to the corpora cavernosa in the penis, causing it to expand. As a result, the corpora cavernosa gets filled with blood and due this pressure, an erection occurs.
I cannot resist but mention here that:
"The lightest thing in the world is the penis. Why? Because even a thought can raise it"In which conditions, this function fails?
This function fails when there is damage to the body tissues required for a proper penile erection, such as the nerves, arteries, smooth muscles of the penis and the fibrous tissue of the penis. Such conditions are:-
1) Nerve Diseases such as in Diabetic neuropathy and Chronic alcoholism giving rise to neuritis. whereby the impulses are not properly transmitted by the nerves.
2) Vascular diseases such as occurring in Diabetes and Atherosclerosis, which result in improper blood flow to the penis due to clogging of the arteries..
3) Kidney diseases cause imbalance in the functioning of the hypothalamus-pituitary gland- gonadal axis resulting in suppression of the gonadotrophin hormone which is essential for reproduction.
4) Certain lifestyle habits such as smoking, obesity and a sedentary life style which all contribute to diabetes and atherosclerosis.
5) Certain medications given for hypertension, depression, peptic ulcer, allergy and for suppressing appetite.
6) Stress, anxiety and a feeling of guilt or a fear of inability to perform sex can cause clouding of the mind which prevents the brain from sending out that signal or impulse.
7) Surgical trauma, to the concerned nerves or arteries or tissues around the penis during surgery on prostrate or the urinary bladder, can occur and give rise to ED.

And of course,
old age where physiological ED sets in.
medic on web.