This is an important question. Thank you for asking it. It is very common and some sources identify that nearly half the men using condoms experience it: You are not alone. It is important to note that in your case this is likely to be psychological rather than physical. If you are able to masturbate with it on — then it is likely to be about you anxiety with your partner.
First look at the transition time. The act of stopping to put the condom on may be the problem. Ask your partner to keep stimulating you (or you do it yourself) while putting it on. Staying stimulated rather than getting hard again may help.
Secondly, consider other forms of contraception, like spermicide, which doesn’t protect you from venereal disease in the way a condom does, but is an effective way of preventing pregnancy.
Finally, keep experimenting with different condoms and talking with your partner. As your anxiety goes down and the skill level goes up you are likely to overcome your circumstance.