In the above picture, the blue team is attacking. When the ball is played forward by a teammate, no "scoring body part" can be beyond the last line of defense for the opposition (not including keeper).
Being even is onside.
If the attacking team is beyond the last line of defense, and the opposing team plays it back to him, he is onside.
If the goalkeeper is further up the pitch than a defender, he becomes the last line of defense, and he is the line.