Priapism is a disease caused by an infiltration of the penis by a material deposit which causes a constant erection; it's a disease of old age.
Occasional (or not so occasional) involuntary erections in the young are usually the result of hormonal influences; they are very frequent in the teens and earlier 20s, caused by an excess of testosterone, and evolutionarily, due to animal instincts to impregnate as many women as possible, and thus to pass on his genes. These still occur in modern times. (The usual life expectancy at that time was late 30s to mid 40s, so it was important to become sexually active as early as possible. During this time, frequent, unexpected erections occurred as a "reminder" to the body that it was time for sex again.)
In later years, such erections become much less common without sexual stimulation; occasionally as a result of build-up of testosterone due to lack of regular release. The stimulation can be mental, as from imagining sex, as from the sight of an attractive woman (or man) with whom a man may, either consciously or unconsciously, wish to mate with.
It should be noted that it is usual for a normal man to have erections several times a night,, usually during REM (or dream) sleep. (There are medical tests which measure such erections, which occur every night normally, to see if they occur; if they do, then it means that there is not a physical cause for impotence, implying that such impotence is psychological.)