At its core, weight loss/gain comes down to calories in vs. calories out (CICO). Calories in, we control through consumption of food & drink; calories out we control via activity (i.e. basal activity or your basal metabolic rate, the energy we expend to function, and physical exercise). I should note — don't take this CICO approach too literally; you shouldn't have to count every calorie you consume. We'll get to this later, but this is a short-sighted approach that depletes your willpower and narrows your focus on the wrong goal. A normal, healthy weight is a part of a healthful, disease-free life — which is the real goal of weight loss. Weight loss, on it's own, is just a way to get there. Keep your eye on the prize.
Related: The Science of Habits
For those just starting, you can try eating less AND exercising more right off the bat, but this is a tall order for anyone starting out on this journey. If you've struggled with sticking to habits, take it step by step, get one thing right at a time, build momentum, and then level up. Lets start with the dietary part of weight loss. There are a few rules I’d live by here, barring any pre-existing conditions or other restrictive diets (e.g. renal diet, diabetic diet, etc.):
Man-made vs nature-made. Eat whole foods over processed foods as often as you can; this helps minimize exposure to additives and empty calories, or ingredients used to help sell a product (texture, taste, smell), but have no nutritional backbone.
Moderation. For people who get adequate nutrition but are still struggling with weight loss, this is by far the biggest problem — overeating. Pre-portioning or eating while drinking water (filling your stomach with non-calorie dense foods) are a few tactics used to solve this. Reiterating from before, this doesn’t mean counting every calorie — this draws our attention away from the real goal: being healthy and living a disease-free life.
Consistency. Stick to a mostly whole-food diet while non overeating — most of the time. Ideally, as often as you can. This doesn’t mean a strict, restrictive diet every day. That’d be a good way to encourage relapse. Give yourself cheat days, but only for good behavior. Earn them. Build a system that encourages gradual progress — stay on a restrictive diet for 2 days, earn your 1 cheat meal. Try it again, and, if you succeed and feel like progressing, then try staying on the diet for 2 days, then cheat meal. Then 3 days, and 4 days on a diet, and so on. The idea is to wean yourself off of needing junk food — by gradually replacing a poor, unhealthy habit with a better one. Minimal wear on your will-power maximizes your return and creates lasting change.
These last few principles apply to the "Calories Out," or fitness, part of the puzzle. There's a lot of nuance in the types of exercises to engage in, but moderation and consistency are both key here. The goal is to build a habit, which means you'll have to stick to something long enough for it to become a habit (popular science has this number at ~21 days, or 3 weeks of continual use). If you exert yourself too much, it'll be hard to continue engaging in the aforementioned habit. Think of it this way: it's much better to do 20 pushups a day for 21 days than it is to do 100/day for only 4 of those 21 days. Choose consistency over maximal exertion, whenever appropriate.