It's a couple things...
First, the scenes are not continuous, despite what you might see in the finished product. A scene actually takes 1-2+ hours to shoot and there are breaks in between. So on one hand that's even more impressive but on the other they get breaks to jack off and get hard again.
Second, the vast majority of porn studs use drugs to keep hard. Viagra, Cialis, or Caverjet injections, whatever it takes.
Third, the guys who last in the business have the mental willpower to get and stay hard for long periods of time. You can be the greatest stud in the world but if you can't get hard on command and stay that way in a room full of people for a couple hours straight, you won't last in the porn biz. That's why pornstars usually work with the same guys over and over again, cuz they have proven themselves capable. No chick wants to sit waiting for a guy to get it up or have to deal with limp dick on camera.