I try to design my encounters so that they are fair and level appropriate, but also enough of a challenge that a pc could possibly die. I play out combat with the intent of killing player characters, while giving the players enough tools to turn the situation to their advantage and get the upper hand.
One way I create difficulty, is with multiple objectives, multiple priority targets, varied opponents (melee, range, magic), and terrain designed with the combat encounter in mind. I provide higher ground for my ranged opponents, flank the players with tough melee opponents, and protect my weaker caster opponents behind bodyguards. I make my monsters use the terrain to their advantage, while allowing the players to do the same. A lot of my combat encounters start with the players making an assessment which are the biggest threats, and there are usually a bunch of surprises along the way. The perfect fight has two phases in my book. The second phase should surprise the players, and motivate them to adapt their strategy.
Since my players try to form strategic formations as well, I try to think of ways in which I might smash those plans to tiny bits. For example, in one of the dungeons my players stuck together a lot, to buff and heal each other, while hiding behind their tank. But I had an enemy that could charge into their ranks and explode, dealing splash damage to everyone in the blast radius. I also used monsters that hid in the shadows of the ceiling, and could drop down and attack the backline if they weren't spotted. I try to be sneaky and cruel to allow my monsters to get the upper hand on them.