Can you please explain the offside rule in Soccer?

Soccer is a sport played between two teams with 11 players each. Out of the 11 players one is a goalkeeper. The offside rule is fundamental in soccer to avoid players from gaining unfair advantage over the opposition. A player is in an offside position if they are closer to the opposition's goal line than the last defender of the opposition team. 

The law states that a player is in an offside position if any of their body parts, except the hands and arms, are in the opponents' half of the pitch, and closer to the opponents' goal line than both the ball and the second-last defender.

Being in an offside position is not an offence in itself, but a player so positioned when the ball is played by a teammate can be judged guilty of an offside offence if they receive the ball or will otherwise become "involved in active play", will "interfere with an opponent", or will "gain an advantage" by being in that position. Offside is often considered one of the most difficult to understand aspects of the sport.