Want to lose fat and gain muscle at the same time? Adding muscle mass requires eating more calories than you burn. Losing fat requires eating fewer calories than you burn. See the problem? That being said, there are a few times you can do both!
Beginners
If you're new to exercise, you'll likely have very little muscle mass and may get modest muscular gains while dieting. This only last for the first few months and then your muscle building will plateau. It's not good to diet for months on end anyways, so that plateau is a good time to go into a calorie surplus and begin bulking.
Drugs
If someone is on steriods, Testosterone, HGH, or other drugs, they're better at building muscle while dieting. Many fitness experts and models are on low doses of drugs in order to keep a lean figure all year and still make muscular gains. They don't talk about it, and most deny it, but now you know to be suspicion of someone touting a magical program for massive body recomposition that "worked for them."
Slow, Clean Bulking
There is conflicting information, but some intermediate lifters say calorie cycling and intermittent fasting over several months still allows them to achieve slow, but steady muscle mass without increasing their body fat percentage. The gains are much slower than alternating bulk and cut cycles, but if staying lean is your priority, exact nutrition and heavy-lifting workouts may allow you to do the same.
Age
The younger you are, the easier it is to build muscle and lose fat... or doing both at the same time. Aging makes both of those harder which means doing them both together is harder still. What you see being espoused by 20-year-old body builders may not work for someone in their forties.
Genetics
Of course, everything in fitness is highly variable based on your genetics. You'll never know what your genes are capable of unless you try. The flip-side of that is that you may not have the greatest genetics either. You may store fat more readily than the next person, your metabolism may be more stubborn, or you may have difficulty gaining mass. Whatever genetic deficits you uncover, you'll also have genetic strengths so look for those too.
Never get discouraged because you're comparing yourself to a fitness model, trainer, or athlete. Most are in those roles because they're in the top 20% of genetically lucky people and have been in some athletic endeavor their entire lives. And those client before/after photos? Don't forget they are marketing cherry-picks. No person or program can guarantee specific results because YOU are unique. Embrace that and think of fitness as self-discovery, not a competition with pictures on the internet.
Alternate Gains
If you aren't able to gain muscle mass while dieting, there are still lots of other improvements you can make during your cut cycle. Stability, endurance, balance, agility, and sport-specific coordination are achievable gains. This extra activity also helps burn more calories, but you won't have the same energy or intensity as you would during a calorie equilibrium or surplus.