In general, I don't set out to surprise the players. Sometimes it will happen because of this occurrence or that, but I find that wanting a particular outcome too much can result in treading upon the players' choices. So the best I can do is set up situations that are interesting and challenging and if the players find themselves surprised by those events, then great! Otherwise, no big deal.
With ambushes, the first thing I will do is some level of telegraphing. The PCs might hear about banditry in the forest or monsters known to stalk the shadows of the forsaken ruins or whatever. While that may seem strange when preparing to present an ambush, it is a form of foreshadowing which creates the context necessary for the players to make decisions to change their fate. Without it, an ambush simply becomes a "gotcha," not unlike the odd trap that the PCs had no chance to avoid except perhaps for a die roll or two instead of solid decision-making. And in any case, players may not do anything in particular to prepare themselves for these eventualities, but if they run afoul of those bandits or stalking monsters and take a beating, they can at least look back and know that they were warned and that their decisions had a hand in the outcome, good or bad.
As far as presenting the actual scene, it can vary based on the NPCs or monsters doing the ambushing. But ultimately it just follows the standard Combat Step by Step rules: The DM determines surprise with the monsters' Dexterity (Stealth) checks against the PCs' passive Perception checks (those who are Keeping Watch, that is) to determine surprise. Any PC who is not Keeping Watch because they've turned their attention to some other task is automatically surprised. The DM then establishes positions, describes the environment, and everyone rolls initiative.
PCs who aren't surprised and also beat the monsters in initiative are aware of where the monsters are, even if they can't see them. The players describe what they want to do and the DM narrates the results of the adventurers' actions. The PCs who are surprised don't get to act in the first round, except perhaps for reactions after their turn has passed.
That's about it. There's really nothing special to it in my view.