When a man is sexually stimulated, the brain sends a chemical signal to nerves in the penis. Those nerves cause a chemical to be released from the blood vessels of the penis.
This chemical then triggers a series of events that causes two large chambers of the penis to fill with blood, much the way water fills a sponge. As the chambers fill up, they block the veins that let blood flow out of the penis and a man is able to get and maintain his erection.
Since getting and keeping an erection depends upon the smooth flow of blood in the penis, certain health issues that affect blood flow can also affect erection quality.