If one puts some food in front of a radiating microwave antenna the food very likely absorbs a part of the wave energy but what reflects off or passes through will be lost forever in free space. Metal walls around the food reflect the waves back and they get a new chance to warm the food.
The walls of the chamber in a microwave owen including the door and its window are metallic and joined together without radiating gaps - at least no radiation at 2,45 GHz is wanted to the kitchen. The design of the door is far from trivial.
There's one hole left: The input. If a wave coming from the input fits well to the dimensions and wall directions of the resonator a substantial standing wave builds up into the chamber. It hopefully has a maximum inside the food.
A part of the wave bouncing forth and back between the walls is caught by the same antenna which feeds the chamber and goes back to the magnetron. The owen chamber should be seen as an extension of the resonators inside the magnetron. The extension hopefully has became lossy due the inserted food, not due radiating gaps nor lossy materials. The losses dissipate the microwave energy in the food.
If the chamber hadn't a resonant frequency near enough the signal frequency the standing wave buildup wouldn't happen. Leak from the magnetron to the chamber would still happen but the field strength maximum in the chamber would be substantially lower than with the resonance.
You may think: "What!!! If I output a kilowatt microwave from the magnetron to the metal chamber it unavoidably dissipates in the food because it's the only dissipating material - there's no need to have a resonator!" BUT: You do not output what you want, you output what the load takes. The rest finds its way back to the magnetron and prevents generating more. The way to maximize what the load takes is to design the chamber to have field intensity maximum in the food and that's a resonator.