The magnetron is vital to a microwave oven's overall high-voltage system. It takes the AC (alternating current) voltage from its electrical power source, increases it to high voltage and then converts it to even higher DC (direct current) voltage. Once that's accomplished, the magnetron converts the newly created DC voltage to RF (radio-frequency) energy. Magnetrons are built to perform this task safely and efficiently, over and over again.
It's a good thing that magnetron modules can usually be replaced, because they can be quite expensive to repair. In many cases, in fact, it can cost more to repair one than to buy a new oven. Fortunately, most magnetrons start at somewhere near $50 and run up to about $250. The latter price would be for a high-wattage, super-efficient module in a high-cost microwave oven brand.
For a variety of reasons, magnetrons may go bad or become damaged. In the latter case, it's usually because some sort of item not meant to be put into a microwave oven ends up in there. Most types of aluminum foil and many metals aren't supposed to be microwaved, for example. When they are, the RF energy the magnetron produces is reflected back to it, damaging it and eventually necessitating repair or replacement.
Before you replace your magnetron, you need to be sure it's what is actually broken. Most digital readout microwaves normally flash a specific fault code that tells you what's broken within them. Consult your owner's manual for codes to determine what yours is. After that, make sure you follow all steps for replacement in the order they're detailed. This is to ensure that the new magnetron doesn't become damaged -- and also for your safety. Consider finding a licensed repair person to do the work.