Well, first things first. You probably aren't going to both lose fat and gain muscle simultaneously. It's possible, but let's not count on it. That being the case, you need to decide whether or not you want to lose fat first, then build muscle or build the muscle and then shed the fat. These are bulking and cutting cycles. This decision is key to determining your diet, not so much your workout.
If you have fewer calories than your body required, generally you'll shed fat. Unfortunately, this means that you don't have enough to spare to build muscle.
If you have an excess of calories, your body will have what it needs to build muscle. Unfortunately, unless you're spot on, you'll probably put on a bit more than just pure muscle.
Which way you go from here depends on what your goals are at this moment. Figure that out and then you can start tweaking your diet.