Why do you lose your erection after ejaculation?

The excess blood which is in the penis goes back to the circulation and the erection is lost. There is a condition called priapism (see link below) which can cause you to have an erection that lasts longer - often this becomes painful.