With me it started a couple of years ago which I initially put down to too much to drink causing it the first time and then performance anxiety. I went to the doctor and I'm physically fine.
I must stress that you should go to the doctor first as this could be the symptom of something else.
The doctor prescribed Viagra which worked, but was expensive as I'm not diabetic and I didn't want to become dependant on the blue pills.
After a while it was increasingly hit and miss without Viagra so I did a search for other solutions online. The advice is broadly for five things:
1) Physical things. Diabetes, heart condition, overweight, unfit etc.
2) Mental things. Stress, performance anxiety etc.
3) Minor physical things. You can take a variety of things (garlic, ginseng etc.) that thin the blood. Also alcohol/any other drugs can cause a problem.
4) Porn. There's so much freely available porn that you can train your mind to require it to maintain an erection.
5) Pelvic floor. A strong pelvic floor can maintain an erection.
So for me, I'd ruled out points one and three (although I have cut down alcohol and avoid it completely on nights that are likely to end in sex). I'm not under stress and my wife has been superb at helping remove performance anxiety, so that's two ruled out as well.
Point five has been important. I hadn't done pelvic floor exercises ever and I think the muscles down there were practically non existant so that was something to deal with.
Point four is the big one. From the stories online, I was a classic case. Start with a bit of porn, then it gets a bit more hardcore. Ultimately I was masterbating to porn about three times a week and had trained my mind that this was the way to erections and orgasm. Unless you're married to a porn star then this is not the same as the experience in the bedroom however attractive and adventurous your wife is.
So I completely cut out porn including erotic fiction. I also cut out masterbation and I'm doing regular pelvic floor exercises. From reading online it can take six months to get rid of a porn dependency. I'm about three months into this now and have had amazing results to the extent that the other week my wife asked if I'd taken Viagra. Now this might not work for everyone which is why I stress to check with a doctor first and have a think about the five points and how they might apply to you, but I would be careful about how, and how often, you use porn.
The good thing about this is that the doctor is happy to prescribe Viagra and now this has become an occasional marathon sex session drug rather than something I need every time.