Its important to go see your doctor and have him give you a check up. There are alot of reasons why maintaning an erection can be difficult.
MEDICATION SIDE EFFECTS
So getting a check up to rule out more serious diseases would be the first step. There are treatments for erectile dysfunctions, so don't dispair.
There is also a big psychological factor to your problem, because now when you are about to have sex you will be thinking about your penis going limp. Try masterbation alone using sexually explicit material and see if you can maintain your erection. Masterbating alone will take alot of the stress of performing well and can hint to weather it is pysical or psychological.
Like jjswartz_1 there are lots of reasons why you can't maintain an erection.
It could be from tiredness, as well as the other reasons already mentioned.
If you have a partner, the thing is is not to worry too much about it. Just accept that sometimes it does happen and that you both have had some pleasure from the act albeit that you didn't get to the end.
Do see your doctor because if you are on any medications, this can be a side effect.
Where you are on any medications or not, it would be a good idea for the doctor to do a blood test to check your testosterone levels. Some men do get low levels of testosterone and there are testosterone gels that are applied to a limb to absorb the hormone.