It's all about calories, I'm sure people before me posted this.
Surplus calories -> you'll add weight
Maintenance calories -> same weight
Deficit calories -> you'll lose weight
It's very simple, yet complicated.....first you want to do trial and error to finding your maintenance with the current activity that you do.
Keep in mind, if you increase activities thus you have to recalculate the maintenance calories again.
#Trail and error
Doing trial and error, just eat and guesstimate of how much u need daily, lets say you're a man whom 6 ft tall, now try first with 2500 calories per week, weight yourself every week. If your weight is the same and doesn't change after one week then its solved.
Surplus add +500 calories to gain weight (if you go to the gym and want muscles)
Maintenance is what you found in the trial and error.
Deficit reduce -500 calories to lose weight.
Lets say during the week of trial and error you found out that your weight changed, its easy...if its up then the 2500 calories wasn't your maintenance and you want to bring it down, if your weight got down then its below your maintenance. It's all math, with little of trial and error.
Now keep in mind if during the trial and error you found your maintenance then you added more activities later on, those activities may require more calories to substitute for them, i.e. lets say my maintenance is 2500 with my normal activity, then 2 months later I decided to add swimming into the mix which I didn't calculate before, now my maintenance may be 3000 calories a day I woudn't know until I do trial and error again (or guess and go with it and see if it is what my goal is set to be).