Losing weight or Gaining weight happens only if you are on calorie deficit or calorie surplus respectively!
However, a healthy change is only when you lose body fat without losing muscles.. or gain muscles instead of gaining body fat.
To lose fat, you need to be in a calorie deficit with right protein intake and physical exercise.
And, to gain muscles, you need to be in a calorie surplus with adequate protein intake and physical exercise (resistance training)
By properly planning nutrition, it is also possible to lose body fat, and gain muscle at the same time.