Whether you're trying to gain weight, lose weight or simply stay where you're at, you need to control your calorie intake. Gaining weight requires that you eat more calories than you burn while losing weight requires the opposite. Your calorie balance (difference between what you eat and what you burn) will determine your weight, there is no way around it.
A calorie deficit is a state in which you are burning more calories than you eat. Losing weight requires a calorie deficit. When you enter this state, your body needs to somehow make up the difference of what you burn and what you eat. If you're burning 2,500 calories per day and eating 2,000 calories, that 500 calories needs to come from somewhere.
That somewhere is body fat. Body fat is stored energy that was created in times of calorie surpluses (read below). When you are in a calorie deficit, your body begins drawing upon those fat stores for energy, which over time, causes weight loss.
A calorie surplus is a state in which you are eating more calories than you are burning. Gaining weight requires a calories surplus. When you enter this state, your body takes the excess energy and uses it to either make you more muscular (if you are working out) or build up body fat and make you fatter.
When you workout, you damage your muscle tissue. This damage is repaired during rest and is what makes you bigger and stronger. The recovery process is a very energy intensive one which is why you need to eat more than burn. The calorie surplus fuels muscle growth. Without this extra energy, muscle gains will be almost non-existent.
Your first step when creating a diet for weight gain, weight loss or maintenance is to figure out how many calories you're burning. The easiest way to do this is by using a tool such as the calorie calculator. This calculator will estimate how many calories your body burns each day based on your size, gender, age and activity level.
Your next step is to eat the correct amount of food. Generally, 3,500 calories is equal to one pound. If you're trying to lose one pound, you need to create a calorie deficit of 500 calories per day over seven days (500 calorie deficit per day x 7 days per week = 3,500 calorie deficit per week). To lose two pounds per week, double it to 1,000 calories per day. The same numbers work for gaining weight but in reverse (surplus instead of a deficit).
The Bottom Line
Remember that for any plan to work, you need to make changes over time. Making changes slowly, both to your diet and exercise routine, will allow your body to get used to your new plan over time rather than overnight which will increase your chances of long term success.