An erection occurs when the brain gets a signal that sexual activity is taking place or thoughts spring up in the brain about sexually stimulating activities. The brain then sends more blood to the penis . This excess blood builds up in the spongy cells of the penis and causes it to get hard in preparation for insertion into a vagina or rectum . Contrary to 'urban legend' there is no mysterious bone in there that comes and goes. It is simply a large amount of blood being pumped in there with no place to go.
Erection is caused by an increased flow of blood to the penis and by a strong decrease in the outflow. Muscles around the penis relax in order to allow the blood into the corpora cavernosa and corpus spongiosum, while contraction of other muscles reduces the outflow. As the tubular structures make full with blood, the penis expands in both length and girth. It also firms and rises to a certain angle.
The sympathetic nervous system is responsible for the failure to achieve erection under conditions of stress. This system interferes with the signals sent by the parasympathetic nervous system, which is actually responsible for activating the erections.
The inability to have an erection suitable for intercourse is called as impotence. May be a result of an injury secondary to radiation therapy, surgical resection of the prostate, hormonal deprivation therapy, or other aspects of neurological, vascular or disease processes.
A less known fact is that erections can occur even after death, due to increased pressure in the penis from sinking fluids or the accumulation of gases.