A player is in an offside position if they are nearer the opponent's goal line than both the ball and the second-to-last defender when the ball is played to them. It only becomes an offence if they then get involved, so simply standing in an offside position is not punished.
Offside has been in the game since the first written Laws in 1863, and its wording has been argued over ever since. A player cannot be caught offside in their own half, or directly from a throw-in, a corner, or a goal kick, and being level with the second-to-last defender counts as onside.
One change shaped the modern game. In 1990 the law was rewritten so that an attacker level with the second-to-last defender is onside rather than off, tilting tight calls towards the forward and encouraging teams to push higher up the pitch.
The principle is simple but the margins are not. Video review now measures the closest offsides to within a few centimetres, which is why goals are sometimes ruled out by the width of a shoulder or a boot, and why the rule is still argued about more than any other.