Gaining muscle and losing fat at the same time has been said to be impossible. The conventional wisdom was that in order to lose weight, we had to be in a calorie deficit and to gain weight(muscle or fat), we had to be in a calorie surplus. This goes in line with the calorie balance hypothesis which we now know is not correct.
Building muscle is simple. Do resistance training that will fatigue the muscles enough to cause a hormonal response. The best way to build muscle is to focus on simple pushing and pulling motion. Heavy resistance and slow, steady lifting until the muscle is entirely fatigued will do the trick. Of course, a good amount of protein is necessary to help repair muscles and rebuild them.
Losing fat is simple as well. We know that insulin is the hormone responsible for storing fat. We also know that insulin is only released when we eat carbohydrates. If we stop eating sugars and starches, our bodies wont store fat. Avoiding carbs or limiting them will make a person lose weight. That means eating a lot of fat and protein wont have an affect on weight.
Putting the Two Together
Coming from a calorie counting paradigm, gaining muscle and losing fat at the same time is impossible. A whole lot of research and a little science takes care of that myth.
Losing fat and gaining muscle at the same time is not hard at all. However, restricting carbs can decrease energy in the short term, so be wary of that when working out. I personally recommend going into ketosis a month before working out to maintain optimal energy.