One of the most dangerous things that can happen on a sailing boat, apart from falling overboard, is for someone to get caught up in the uncontrolled gybe of a sail. You may be hit by the boom, but even getting in the way of the sheet has been fatal. Quite apart from that, uncontrolled gybes can bring down a mast or lead to other serious gear failure.
Rigging a preventer on a yacht's mainsail should be normal procedure whenever cruising with the wind behind the beam (i.e. when it's coming from more than 90° off the bow). It can also be useful at other times when you have more swell than wind, so the wind may not have the strength to keep the boom in place as the boat dips and rolls.
On any boat that is sailing downwind without a preventer, strict 'heads-down' procedures must be enforced anywhere within the boom's arc. Certain areas of the side-decks and maybe the cockpit also have to be strictly 'no-go' to all crew depending on what the boom and mainsheet could do there in unchecked full swing.
Commercial products are available that operate in various ways, but the simplest and strongest preventer is a line, from the end of the boom, led outside of the shrouds and a long way forward - perhaps right up to the bow - through a block, back to the cockpit and secured within reach of the mainsheet .
If the boat is to be gybed, then a second identical preventer line must have been rigged on the other, windward, side. Carefully, using the steering, the stern of the boat is brought up into the wind. Then the leeward, active preventer is released little by little, while the mainsheet is shortened to bring in the boom. It is important to maintain at least a turn or two around the preventer's cleat the whole time ready to catch an early gybe during this stage of the manoeuvre. Make the mainsheet pull the preventer around its cleat, do not offer it any slack. All the while it is also necessary to take in slack on the other, lazy preventer to keep it under control (i.e. prevent it getting tangled around something) until it is needed.
As the boom gets as near as you can get it to midships (near to running fore-and-aft along the boat's centreline), slacken the active preventer, tighten the lazy one, make sure the mainsheet is very secure, check that all crew are safe from where the boom may swing, call "Gybe-ho" if you're feeling nautical, then make the slight steering adjustment that will actually gybe the sail. The course of the boat may slew further than you expect: That's good, ignore it, it gives you time to do the next three things.
Run out the mainsheet as fast as you can without burning your hand, check that the newly lazy preventer runs free, tighten in and secure the newly active one.