It is sad that this isn't common knowledge. The fad diets, weight gain products, and general false advertising to people who want to change their body image have led to a lot of confusion about how to really gain or lose weight. The simple truth is that you need a certain number of calories to support your body's activities for the day. If you eat more than that, you will gain weight. If you eat less, you will lose weight. To find out how many calories you need in a day, check out this calorie calculator. If you read the label on the food that you buy, you'll be able to see the number of calories in the nutritional information box.
How much extra do you need to gain weight? If you want to gain 1 pound of fat, you need to eat 3500 extra calories. If you want to lose 1 pound, you need to eat 3500 than your body needs. This is only a rough estimate because your body will also build/use muscle when you gain/lose weight.
I am personally trying to gain weight so the number of calories I am eating is 20x my body weight in pounds. Today I am 155 lbs so I am going to eat 3100 calories today. According to the calorie calculator, I need about 2400 calories given that I workout 3 times a week. So my calories excess is (3100 - 2400) = 700 calories. That means that I'm going to gain roughly a fifth of a pound or one pound every five days. This might not seem like a lot, but remember that my goal is muscle gain, not fat gain.
At the same time, you might be thinking that 3100 calories seems like a lot to be eating. Trust me, even for a hard gainer, this is possible. You don't need supplements or whey protein to make it happen. If you eat when you get up and consistently throughout the day, you can get all 3100 from regular food. However, there is an easier way: WHOLE MILK. The whole milk that I'm drinking has 150 calories per cup which is 300 calories for a regular glass of milk. If you drink a couple glasses of milk with every meal, you'll have a much easier time getting all your calories. If you want to really aggressive about gaining weight, you can drink as much as a gallon of milk a day. This should be accompanied by strength training. Without strength training, a majority of the weight you gain will be fat, but with strength training, your weight gain will be almost entirely muscle. Although I am going to go into more detail about the type of training that you should do, I feel need to say here that strength training should use compound exercises like squat, bench, deadlift, push press, and barbell rows. Isolated exercises like tricep pulldowns, bicep curls, calf raises, etc... are going to do little or nothing for your overall strength.
If you're trying to lose weight but not muscle, you should eat close to your daily caloric needs and strength train at the same time. Strength training uses energy for two things: the act of lifting the weight and muscle recovery. Muscle recovery will actually use more energy than the lift itself since recovery continues for hours and days after an exercise. So if you eat only what you need and do strength training, your muscles will use the fat in your body for recovery resulting in losing fat and gaining muscle at the same time.