The pilots wear a special "G" suit that forces the blood from the lower part of their body upward to help prevent passing out. That and training on how to breathe and tighten certain muscle groups enable them to do so.
I was fortunate to spend a week aboard an aircraft carrier and had some cool talks with F-18 pilots. I would take a ride with them in a heartbeat. I did get to do a carrier landing and takeoff on another trip aboard the carrier, not on an F-18 though. Still wicked cool.