The Invisible Boundary of Soccer
You are offside if any part of your scoring body is closer to the goal than the second-to-last defender. It is not about the whole player, but rather any part capable of scoring a goal that breaks the line.
This rule is designed to prevent players from simply camping out next to the opponent's goalkeeper. Because it counts parts like your head or feet, a goal can be disallowed even if your torso is behind the defender. The goalkeeper usually counts as one of the two defenders, so you typically need two opponents between you and the goal when the ball is played. It turns attacking into a complex game of timing and spatial awareness.
Source: Offside (association football)