A band of adventurers sneaks up on a bandit camp, springing from the trees to attack them. A gelatinous cube glides down a dungeon passage, unnoticed by the adventurers until the cube engulfs one of them. In these situations, one side of the battle gains surprise over the other.
The DM determines who might be surprised. If neither side tries to be stealthy, they automatically notice each other. If one side is trying to get the drop on the other, however, the DM compares ability checks made by both parties to determine surprise. In most cases, this will call for a Dexterity (Stealth) check from anyone attempting to hide with the passive Wisdom (Perception) score of each creature on the opposing side (assuming they are not actively alerted) or Wisdom (Perception) check if the opposing side is actively searching. In other cases, like suddenly attempting to attack an opponent during an otherwise cordial social gathering, this might be adjudicated by comparing the attacking character's Charisma (Deception) check with the target's Wisdom (Insight) check.
Any character or monster that fails the relevant check fails to notice the threat and is surprised at the start of the encounter.
If you're surprised, you can't move or take an action on your first turn of the combat, and you can't take a reaction until that turn ends.
A member of a group can be surprised even if the other members aren't.