Eating plays a major part too. (If you are in a deficit, you won't gain weight, if you are in a surplus, then you will gain weight, (this is base science).
Not quite. There are numerous physical conditions that actually defy this rule rather powerfully. I have intimate experience with one of them: hypothyroidism. You see, it's not as simple as energy in, energy out. There are endless chemical processes that take place in your body the second food passes your lips and all of them must work correctly for you to actually benefit from the food you eat. If even one of those processes is out of whack, the whole system can tumble and the body can suffer.
Most people are blissfully unaware of the elegant biological firestorm that's activated by eating because it's working okay for them. But some of us get a painful wake up call. Some people's stomachs don't break the food down well enough, others can't absorb the food from their intestines in an efficient manner (talk to CellarYeti in here, he's got a ton of information on this one), still others can absorb the food but they can't eliminate wastes correctly so all their bodily energy is spent fighting illnesses created by a build up of toxins in their bloodstream.
The most sneaky and annoying stage for things to go wrong is at the cellular level. It's the cells of your body that you're eating for and they receive the benefit of it by allowing nutrients to pass through their cellular membranes to be processed in their mitochondria and other organelles. If they aren't absorbing, you aren't taking in energy. You may be eating but you aren't receiving the benefit. The energy just moves around the body until either several types of immune system cells destroy it as imposters or fat cells take it in and store it. So, you can eat and eat, receive no energy from it, starve on a cellular level while all your organs suffer (including your brain, which needs lots of energy), and still put on fat.
That is exactly what happens in hypothyroidism and it is a living hell. A (not)fun fact about hypothyroidism: it can hit you in degrees. You can have a mild form of it, it's not all or nothing, and doctors miss most of the mild cases. I was lucky enough to be the type who never gives up and after doing years of my own research and experimentation, I figured out which stressors were causing my cells to reject nutrients. I reversed it and have been healthy ever since.
Are you ready for the kicker? The cellular level is where most diseases begin. You start with some sort of stress, usually mental, it puts pressure on your cells, they buckle after a while and manifest a disease or condition, and then a doctor tosses MORE stress at your body in the form of drugs or cutting into you. The whole mess can be put to rest if the person simply addressed and eliminated the original stress months--or decades--ago.
I'd like to start a movement wherein people stop bragging about how much they suffer more than the next guy and instead clap for one another when each one of us wins. My god, the hospitals would be so empty, they'd have to come up with new diseases and new drug treatments just to keep the revenue flowing in. Oh, wait a minute....