But I what I don’t get is why these other factors are not significant:
the energy in through your lungs, by breathing
You do not actually get energy from breathing. You get oxygen, which is used by muscles, organs, etc in the continuous cycle of breaking down food or fat into usable energy or storage. (Think of it as gas for the body). The basic path is below, building blocks go into the energy cycle, and out the other side comes energy and waste products.
Food + water + oxygen --> Krebs cycle --> cell energy, water and carbon dioxide.
the mass in through your mouth, which would be measured in grams not calories
It's measured in both, actually. Food has a weight, as well as a calorie amount.
the mass that goes out of my body, when I poop
the energy that goes out of my body, when I poop
potential variation in the above two quantities, if for instance the human body “keeps” more or less of what it eats depending on its needs, its preferences, its general efficiency, etc..
Ok, at the very basic level, your body essentially runs on sugar. Everything your body does in a day to just stay alive requires a certain amount of energy. This energy total is called basal metabolic rate (BMR). If all you do is lay in bed and breathe all day (Other than bathroom breaks), you will burn X calories worth of your sugar storage (Glucose/glycogen).
If you get up and clean the house, you will burn an extra bit of calories. If you eat something, you burn calories chewing and digesting. If you dig a ditch, you will burn calories. Add all of that up, add it to your BMR, and you get the Total Daily Energy Expenditure (TDEE).
If the amount of potential energy (calories) that you eat is bigger then your TDEE on a consistent basis, you will gain weight. It might be fast or slow, but you will gain weight. Conversely, if you eat fewer calories than your TDEE, you will lose weight.
Now for your grams/calories, grams is a weight measure only. You can have 100 grams of broccoli, and it has the energy potential of 34 calories. Mostly because it is carbohydrates and undigestible fiber. Chips Ahoy cookies, 100 grams, has 480 calories of energy potential. Some of this is lost in the digestive process, but because of the fat and sugar content, it has a higher calorie count.
Finally, the reason people get fat when they eat too much too often, is that carbohydrates and proteins are approximately 4 calories per gram. Fat is approximately 9 calories per gram. You're getting a little more than double the energy for a gram of body fat. So, if you eat in excess of your TDEE, your body will store at least some of that extra energy as body fat. Conversely, if you eat less, your body will dip into body fat stores to make up the difference.
This is a very stripped down view of a very detailed topic, so let me know if you need any clarifications.