Put some vinegar in a plastic bottle, drop in a small paper packet of baking soda, and screw the cap on tight. Weigh the bottle: say 250.0 g. Shake it so the two mix. It fizzes inside as carbon dioxide forms, yet the scale still reads 250.0 g. Now open the cap. Gas hisses out, and the reading drops. New substances formed while the cap was on, but nothing entered or left, so the mass stayed the same until the gas escaped.
The reason is that a reaction only rearranges atoms. Every carbon, oxygen and hydrogen atom that goes in comes out, joined to different partners. Atoms of an element always have the same mass, so the same atoms add up to the same total mass. That is why 6 g of carbon and 16 g of oxygen make exactly 6+16=22 g of carbon dioxide.
Mass is conserved, but the number of molecules is not. In 2H2+O2→2H2O, three molecules become two, yet the total mass is unchanged because the atoms are the same. Volume is not conserved either: mix 50 mL of ethanol (the alcohol in hand sanitizer) with 50 mL of water and you get a little less than 100 mL, although the masses still add exactly. When a problem is about mass, add masses; do not expect molecule counts or volumes to add the same way.
Think of it like this. Picture a sealed jar of coins. Shake it and the coins land in new piles, but the jar weighs the same, just as atoms regroup without changing the total mass. Open the lid and let a few coins roll out, and the jar gets lighter, which is what happens when a gas escapes. The difference is that escaping gas is invisible, so the loss is easy to miss.
Core idea. Atoms are neither made nor destroyed in a reaction, so the total mass before equals the total mass after. When a measured mass changes, something crossed the boundary: a gas escaped, or oxygen came in from the air.