This is a common question with no simple answer. It IS possible, but not easily accomplished, particularly for fit individuals who exercise regularly. Individuals who are overweight or new to exercise, or both, can initially lose weight while gaining muscle at the same time. For regular exercisers in good physical condition, however, this is more difficult. It also has to do with genetics- some people can bulk up or slim down more easily than others.
In order to lose fat, you must burn more calories than you eat. In order to build muscle, you must eat more calories than you burn because muscles need fuel to grow. Doing both at the same time is tricky, but it IS possible.
The calorie surplus necessary to build muscle can be taken from stored fat in the body rather than from eating more food. Stored fat is stored energy. Every gram of fat burned releases energy the body can use. This energy can be the surplus needed to increase muscle mass while still allowing for fat loss.
Aerobic exercise sheds fat by increasing energy expenditure  while resistance training increases muscle mass. You build up muscle by overloading it. Both strength training and cardio workouts create resistance, either with weights or body mass. When the resistance is sufficiently demanding, muscle tissue responds by increasing in size and changing shape.
Studies show it is possible to simultaneously build muscle and lose fat.  A 1998 study published in the Journal of Applied Physiology found that strength and cardio training done together has beneficial effects on both energy expenditure and weight loss. Each of these alone, (strength only or cardio only) also have benefits, but to a lesser degree than when combined.
This study, along with countless others, emphasises the importance of HIIT training which alternates high intensity cardio exercises with strength training exercises. This allows your body to lose fat through cardio, while simultaneously gaining muscle through strength training. In addition, HIIT fitness requires less time than longer cardio workouts.
To find simple HIIT workouts that require no equipment, go to ShortRadomWorkout.com
1. Dolezal BA. Et al. Concurrent resistance and endurance training influence basal metabolic rate in non-dieting individuals. J Appl Physiol (1985). 1998 Aug;85(2):695-700.