Normally the male penis is soft and flaccid, but if sexually stimulated it becomes firm and erect (tumescent). This is a reflex that cannot be consciously controlled by the man, and in fact if the man does try to consciously control an erection it is more likely to fail.
Stimulation of the penis, other sensitive areas of the body (eg. nipple, small of back) and mental sexual imagery will result in a reflex in the nerves at the lower end of the spinal cord that sends a signal to muscle rings (valves) around the veins in the base of the penis that drain blood from the organ. These valves close, preventing blood from escaping from the penis while blood continues to be pumped into the organ through the arteries as normal. As a result it blows up in the same way as a sausage shaped balloon, the pressure of blood within the penis being the same as the maximum blood pressure elsewhere in the vascular system.
The penis has a long sponge filled sac (corpus cavernosum) along each side that fills with the blood under pressure to support the organ when erect.
When ejaculation occurs or sexual stimulation ceases, the valves around the veins open and allow the blood to drain out of the penis, and it becomes soft again.
An inability to obtain an erection is called impotence.