See it coming!
The most common scenario is to lose balance progressively, either while riding along a difficult line, or just after landing a transition. If the ground is not far off, or there is enough support around, then you just need to extend your foot nearer the ground to dab. End of the story. If you are off-balance while on a fairly high obstacle, and there is nothing near enough to reach with your foot, then the safest option is to completely jump off the bike while holding onto the handlebar. Clear both feet off the frame, on the side you are falling off. Very often, you can land on your feet and save the bike from a sound trashing by keeping the bars in hand. Also, holding the bike can help you keep your balance when landing on your feet (forming a tripod with the rear wheel as it touches ground). When trying out a new line, try to visualize it as a perfect move, but also try to identify the crash scenarios and the possible bail out options. That's a tiny mental preparation that will help you assess the risks you are willing to take. Then focus again on the perfect move to comfort you in a positive approach of the obstacle.
Use the bike's inertia to correct your fall
Now, if the fall is from high (say more than your height), let the bike go completely or even better, push it away from you to land freely on your feet (then you can roll away or crouch and stop your fall with your hands). While you are ejecting yourself, use the bike to support your ejection. As long as the bike is touching the obstacle, it offers some kind of support you can use to eject yourself away from danger (taking out the frame away from between your legs, basically).
Bailing out over the bars
If you are off-balance forward, and again in the scenario where there is nowhere obvious in front of you to put your feet and stop your fall, then jump over the bars and try to land on your feet. In this case, eject yourself by pushing down on the bars and let the bike go completely once you have ejected clean. Rather than toppling forward, try to keep you head up and your feet down, grabbing the obstacle or any edge at hand to dampen the final impact. If you are still plunging forward (after a sudden chain snappage for example), heading for a nasty crash and it's too late for a complete bail out, let the handlebar go and extend your arms in front of you to take the impact. Reception on your hands will prevent some nasty facial reception (nose bleed, broken teeth, jaw snapper, chin cuts). Believe me, you'd rather have a sore wrist than head-butt a rock (even with a helmet).
Bailing out backward
In some cases, you may land on an obstacle just too short to maintain your landing position. If you are falling backward and not too sure about backing off with a clean backhop, just take your feet off the pedals to land on your feet rather than on your back. Again, if its from fairly high, you may not be able to keep the bars in hand, especially if the bike is stuck on the obstacle. Then let go. Alternatively, if you really don't want to trash your bike to the ground, the safest way to keep control of the bike while bailing out can be to grab the tope tube of the frame (you minimise damage, but you can still land on your feet).
If you were about to drop off or pedal kick, or you already kicked in the pedal, but at the last second you have a bad feeling about it,
your last chance is to let the bike go while sticking your bad foot (the non-kicking one) on the
obstacle you were about to leave. If you are lucky, you'll be able to stay on the edge and even hold your bike after the kick.
If you are not so lucky (its all about how early you saw it coming and how fast you reacted), then you may end up doing
the gap jump or drop off, but off your bike, standing in the air and holding your bike in front of you (a kind of vertical superman).
If it still looks scary, then push the bike away from you to land on your feet, without the frame in the way.
Basically, you want to bail off when it gives you a clear safety advantage. In biketrials, because there is rarely any speed involved, you are often better off landing properly on your feet than off-balance on the bike. In other biking styles, like freeride or bmx riding where big jumps involve quite a lot of speed and momentum, and if you end up off-balance in the air, bailing off is not necessarily the best option. You'll be better off making use of the huge freeride suspensions or landing on the bike at speed and then crash or bail off a few seconds later (the future is bleak anyway). For fast jumps, the only times when you should clearly eject off your bike to crash-land on foot is when you realise you are going to land too short anyway (nose-pick into a jump lip or into a gap). Having the bike between your legs is just not helping.
Wear a helmet
It can be bad enough when you see it coming, if you don't bail out properly, but some crashes are so unforseeable that only sheer luck will save you from nasty damage. Actually, about luck, you choose how much of it you put on your side by evaluating your competence and self-confidence over the challenge at hand. The risks only exist if you are willing to take them, so learn to know your limits to work on them progressively. The most common mechanical failures that lead to serious crashes are a chain that snaps and brakes that give off (pads flying off, hydraulic hose bursting etc..) Less common is the sudden bar failure when pulling out a pedal kick, or the bottom bracket splitting into two bits upon landing. Well, check out the trash zone for more variations on the theme. Limit damage. Wear at least a helmet and some good gloves. Shin/knee pads are another must, unless you like the occasional pain of some grippy pedals digging into your flesh. Check out the crash page.