The following diagram shows how an erection is mediated and how sildenafil acts in the body. Nitric Oxide (NO) is released with sexual stimulation from nerve endings and endothelial cells in the spongy erectile tissue, the corpus cavernosum of the penis. The enzyme guanylate cyclase then converts guanosine triphosphate (GTP) into cyclic guanosine monophosphate (cGMP). cGMP causes the smooth muscle to relax, which causes an inflow of blood which then leads to an erection. cGMP is then hydrolysed back to the inactive GMP by phosphodiesteras type 5 (PDE5).
The levels of cGMP are therefore controlled by the activation of cyclic nucleotide cyclase and the breakdown by PDE5. It is the latter that sildenafil acts upon. Men who suffer from erectile dysfunction often produce too little amounts of NO. This means that the small amount of cGMP they produce is broken down at the same rate and therefore doesn't have the time to accumulate and cause a prolonged vasodilation effect.
Sildenafil works by inhibiting the enzyme PDE5 by occupying its active site. This means that cGMP is not hydrolysed as fast and this allows the smooth muscle to relax.
Sildenafil is a potent and highly selective inhibitor of PDE5.

If viewed in cross section, the glans of the penis reveal that the penis consists of three tube-like projections of spongy tissue, the corpus spongiosum, located ventrally and the paired corpi cavernosi located dorsally. In each of the latter is the deep artery of the penis which carries blood over the length of the penis into the open channels that make up the corpus cavernosum. The blood carried out of the corpi cavernosi empties into the dorsal vein of the penis which then returns the blood to the body. It is thought that the level of rigidity of the penis is due to the relationship between arterial inflow and venous outflow in the penis. This means that the larger the calibre of the arteries, the more blood enters the corpus cavernosum and enlarges the penis, and the larger the calibre of the veins, the more blood is shunted away from the penis. An erection is an involuntary action controlled by the autonomic nervous system which consists of the sympathetic and parasympathetic pathways. In general, sympathetic stimulation leads to the constriction of smooth muscle surrounding the arteries (reducing the calibre) and parasympathetic stimulation induces smooth muscle relaxation (larger artery).
When stimulated sexually, the sympathetic stimulation of the penis decreases and the parasympathetic stimulation increases. It is the terminal of the axons of the parasympathetic nerves that release NO. The mechanism that causes erection is then the one shown above. The engorged sinusoids of the corpi cavernosi compress the penile veins, which reduces blood outflow from the penis. Blood is therefore trapped in the penis, which maintains the erection.

The cycle of transformations of guanosine triphosphate is shown in more detail below (GC-Guanosine cyclase):

Sidenafil is selective for the enzyme phosphodiesterase of type 5 : its effect is more potent on PDE5 than on the other known PDE enzymes. It is 80-fold more selective for PDE5 than PDE1 and over 1000-fold more selective for PDE5 than for PDE2, PDE3 and PDE4. However, its selectively is only 10-fold as potent for PDE5 compared to PDE6 (found in the retina) which could be the basis for abnormalities related to colour vision (blue, green) which have been talked about a lot in the media. PDE5 is also found in lower concentrations in the platelets, vascular and visceral smooth muscle and skeletal muscle. Sildenafil inhibits PDE5 in these tissues as well, which is the cause of several side effects which are believed to be due to Viagra. There are 7 types of phosphodiesterases (or isozymes). They catalyse the same reaction, but the isozymes show show different behaviour when subjected to physical biochemical techniques.
Those 7 types of PDE are summerised in the table below. The Km values are equivalent to measurements of the tightness of binding (or affinity) of enzyme (PDE) to substrate (cGMP). The smaller the Km value, the more tightly the substrate binds to the enzyme.

Tissue/cellular Distribution
I calmodulin-dependent 1-30 3 brain, testis, sperm, coronary artery,
lung, heart, pancreas
II cGMP stimulated 50 50 brain, adrenal gland, heart
III cGMP inhibited 0.2 0.3 T lymphocytes, macrophages, platelets,
smooth muscle, heart, adipose tissue
IV cAMP specific 4 >3000 monocytes, leukocytes, central nervous
V cGMP specific 150 1 lung, smooth muscle, platelets,
VI photoreceptor specific 60 2000 retina of eye
VII high affinity, cAMP specific 0.2 >1000 ?

Because of the way in which sildenafil acts, it has no effect in the absence of sexual stimulation when used at the recommended doses.
The enzyme with which sildenafil interacts, phosphodiesterase type 5, is represented below, along with cGMP which is hydrolysed by PDE5.

of PDE5

of cGMP