The answer to that question has varied considerably throughout the years.
The earliest answer was, well, find the softest thing in your line of sight and try to run into it as slowly and gently as possible. That "solution" was equally unpopular both among pilots and owners of haystacks, so the search was on for a better idea. They didn't have to look far.
Surprisingly, the parachute was invented very early, indeed. The modern parachute was invented by a Frenchman, Louis-Sebastien Lenormand, in 1783. If the year sounds familiar, it should; the first hot-air balloons were also invented around the same time, and necessity drove balloonists to find a ... slower descent should something unexpected happen. Despite this, it took several years after the invention of the airplane before parachutes were successfully adapted to the new machine. This is partly because an airplane's cockpit affords far less room than does a balloon's gondola. Eventually, the mechanics for packing a parachute within a pack worn on the back were worked out. Within ten years, aviators had a more-or-less reliable way to part company with a misbehaving aircraft, and live to brag about it afterwards. They're still the cornerstone of all escape mechanisms. But only the cornerstone. As aircraft flew higher and faster, the parachute would need a little help.
The problem that you face bailing out of a high-speed jet is simply this: you're not strong enough to overcome the blast of air howling outside the cockpit. You may find the solution either obvious or counter-intuitive, depending on how naturally you look to explosives as the answer to all your problems. Basically, if you're not strong enough to leap clear, you light off a solid rocket motor under your seat and let chemistry take it from there.
The early models ... didn't work so well. But they got better. Early seats had a minimum "safe" altitude, while modern seats can be used from an airplane sitting still on the ground, if necessary. (I'll admit, I'm having a hard time imagining when it'd be necessary to punch out of an aircraft sitting still on a runway. But I digress.)
This arrangement works well, up to a point. That point would be somewhere north of Mach 1, when the hammer-blows from the oncoming supersonic airstream can deliver a lethal beat-down to a man whose only protection is a flight suit. And that's to say nothing of the problems posed by the lack of oxygen at extreme altitudes. Three different methods have been proposed for dealing with this problem.
When drawing up what would eventually become the F-111, engineers at General Dynamics decided that since throwing a flight-suited man into a supersonic aistream was the problem, they just wouldn't do that. If getting him out of the cockpit was the problem, they figured they'd let him take the cockpit with him. In case of emergency, the F-111's crew cabin would separate from the rest of the aircraft, and descend under its own parachute. Once on the ground, the pilots could just walk out.
It works much better than it looks.
A similar escape pod was originally planned for the B-1A supersonic bomber, but when it was re-designed into the B-1B, the supersonic dash requirement was removed, and it was decided that ordinary ejection seats would serve just as well. While escape pods work quite well, they're very heavy, and thus very expensive.
Another group of engineers at General Dynamics, then called Convair, worked on a slightly different solution. Instead of ejecting the entire cabin, perhaps an enclosure that covered only the ejection seat would serve? This was the system they worked out for the B-58 supersonic bomber. Each of the three ejection seats had a clamshell door overhead. In the event of emergency, the shell would snap shut, protecting the crewman from the supersonic blast of air just outside.
This also works a lot better than it looks.
The capsules even had a set of controls, so that the pilot could attempt to keep the aircraft under control while his crew punched out. It wasn't a bad idea, but it does impose a weight penalty above and beyond ordinary ejection seats.
And then you had Kelly Johnson at Lockheed, who wasn't having any of that "enclosure" nonsense.
Because enclosures are for sissies.
No, when they built the A-12 and its follow-on SR-71 Blackbird, they'd put their crew in a full space suit, and have them sit in ordinary ejection seats. The suit would take the abuse of a Mach-3 aerodynamic beat-down, and also provide insulation and breathing air while the crew descended towards Earth. The few times its had to be used, it worked fine. The one time they lost a man post-ejection, it was due to drowning, not anything that happened at altitude. The thing Johnson liked best about this arrangement is that the weight penalty was almost negligible, being that they had to put the pilots in pressure suits anyway due to the aircraft's operating altitude.
And then, we come to the final frontier. The same question remains: what do you do when your spaceship quits on you, and you're still up in orbit? Take heart, my friend, for the engineers at General Electric have you covered! Or don't, because their brainchild, MOOSE, has been called "the single most terrifying form of transport ever devised by man." Basically, when things go cubist, you bring a suitcase-sized thing outside with you and open it up. You strap it on your back, then pull a cord to fill a cone-shaped shell with foam. Then, you use a hand-held gas gun to point yourself in more or less the right direction, before you light off a retro-rocket for re-entry. Then you spend the next half-hour desperately praying that you didn't forget to carry the one, because if you got any of that sequence wrong, your butt was gonna roast like a Thanksgiving turkey.
What could POSSIBLY go wrong?
No one especially liked this plan. Not NASA, not the Air Force, not anyone involved with sending astronauts into space. Once it was clear that no one was buying, GE basically shelved the whole idea.
Again, I'd like to remind you that flying is still the safest way to travel. Unless, of course, your trip involves supersonic flight or a voyage through outer space, in which case you knew the job was dangerous when you took it.