The blood flow in the penis (the inflow and the outflow) determines erection. Given above is a picture of the anatomy of the penis. The penis has a shaft and a head called the glans. In a cross section there are three tubes inside the penis. Two of them are vascular (carry blood) and are called the corpora cavernosa. One in the middle, the corpus spongiosum, is penetrated by the urine tube (urethra) and carries urine and or semen. In order for erection to occur the cavernosa have to be filled with blood. This helps in achieving the erection. The erection thus achieved needs to be maintained and the blood that enters the cavernosa is prevented from leaving by the expansion of the highly elastic sinusoids (vascular spaces) and compression of these spaces against the coverings of the penis.
Erectile dysfunction occurs either because the blood does not enter the vascular spaces adequately (causes delay in achieving erection) or there is fibrosis of the vascular space which prevents adequate expansion and thus results in the blood running off. This causes difficulty in maintaining the erection.