Balloon analogy
Think of a balloon that is partially filled with water, partially with air. The balloon wants to contract a small amount.
If we hold the opening of the balloon downward, at least below the water line, what happens when the balloon contracts? Water comes out. In the end, there will be the same amount of air in the balloon, but less water.
If we hold the opening of the balloon upward, at least above the water line, what happens when the balloon contracts? Air comes out. In the end, there will be the same amount of water in the balloon, but less air.
If we hold the opening of the balloon along the water line, what happens when the balloon contracts? A mixture of air and water comes out.
Burping a baby
Babies ingest air while feeding. That air sits in their stomach, and there's only two ways for it to go: coming back up as a burp, or going into their bowels.
Gas in baby's bowels leads to cramps which can be a major cause for babies crying incessantly and being unable to sleep, which leads to overtiredness and even more crying.
We want the air to leave their stomach via their esophagus, which is what we call a burp. To do so, we sit the baby upright, and we put some pressure on their stomachs using gentle patting, to coax the stomach into relieving some pressure. If there is air in the stomach, there is going to be some pressure, and our bodies try to keep the stomach contents inside. What we're trying to do when we burp a baby is to "lift the lid" that hold the stomach shut, enough that the air can escape but not enough that the baby throws up their entire stomach contents.
The gentle patting also helps the air bubbles to bubble up to the top of the stomach, in case they were trapped in a stomach fold somewhere. This is similar to how tapping a glass containing a fizzy drink causes the bubbles on the side of the glass to bubble up.
Just like the balloon analogy, by holding the baby upright, you ensure that mostly air comes up and not the milk which we want to remain inside the stomach.