I have had the same problem before. It may be from you being nervous. Try to relax.
Also, the thought of wearing a condom has always been a turn off for me, and sometimes at the moment I need to put it on, it has caused me problems keeping an erection. Relaxing and gaining some confidence with my partner has really helped me keep my erection.