Here is a simplistic explanation of how it works:
Calories consumed in a day must equal the calories expended in a day to maintain your current weight.
Calories consumed in a day must be less than calories expended in a day to lose weight.
Calories consumed in a day must be greater that the calories expended in a day to gain weight.