Planes undergo extensive testing, including putting the landing gears under all kinds of stress. In some planes, if the gear can't be lowered in the normal fashion, pilots can try a gravity drop where the doors are open and the gear drops into place via gravity.
Pilots are trained to handle emergencies such as a gear failure. The exact procedures vary by airline and plane type. They can include doing fly bys so crews on the ground can assess the problem; lowering the gear by other means; burning off/dumping fuel to minimize the fire hazard; have emergency equipment on standby; and landing in such a way as to stay off the broken gear as long as possible.
A plane with broken landing gear is obviously an emergency, but the recent incidents that I can recall were all non-fatal. The good thing is that pilots and ATC usually have time to examine the situation and do everything possible to minimize damage before the landing is attempted.