What gives me a penalty for leaving?

You get an XP penalty if you leave a certain percentage of your last 20 games. Testing has shown that that you will be shown a warning if you've left 3 of your last 20 games. From Blizzard's patch notes, leaving one more game will cause you to get a Leaver Penalty.

This can be for any reason, including game/computer crashes. You should be able to leave during the hero select screen as long as it's before the game's Setup time has started.

A match is considered to be completed as soon as the words Victory or Defeat appear on the screen. This is when XP is actually awarded as well... you can tell this because the achievements for levels 10, 20, and 50 are awarded before the Play of the Game appears.