You're free to leave a match of Overwatch whenever you wish, but doing so enough times can lead to a penalty. The specifics of how this work aren't explained inside the game, so Blizzard has offered a fairly detailed breakdown of how and when you'll be penalized.
Overwatch constantly looks at your 20 most recent games and compares the number of matches you've played with the number you've actually completed. If you're leaving too many and this percentage drops behind Blizzard's required threshold (the exact number wasn't shared), you'll be warned about leaving games too frequently. This message appears in bottom-left corner of the screen.
If you persist in leaving games after being warned, you'll be hit with a 75 percent XP penalty in future games. Leveling up is what rewards players with loot boxes, so that's nothing to scoff at.
This penalty remains in place until you've played enough games without leaving to bring that percentage back over the required mark. This is still calculated on your 20 most recent games, so it shouldn't be a tremendous burden to get back into Blizzard's good graces.
Lest you think you can circumvent this leaver penalty by disconnecting your Internet or getting yourself kicked for being idle, these things are still considered leaving. As such, you'll still end up being penalized if they happen often enough.
There are two times where you're safe to drop from a match: during the Assemble Your Team stage (before your team spawns in) and after seeing the Victory or Defeat screens. Quitting during the Setup phase treats you as a leaver.
"Leaving a couple games in a row won't cause you to be penalized in any way," Blizzard says. "We understand that there are circumstances, either in or outside of your control, that may necessitate you needing to step away from a game before it ends. It's only if you repeatedly leave games in succession, to the point where it could be considered a habitual behavior, that you will be penalized."
It also adds that the threshold to trigger the XP penalty is "actually pretty high."
Blizzard clarified one unfortunate way you can be treated as a leaver: accepting a group invite while in the middle of a game. Doing so causes you to immediately quit the game you're in and join up with your group. If this happens outside of one of those safe times to leave, this will count as you leaving the match. The company says it's aware that some players take issue with the way this system work, stating, "[T]hat's certainly feedback for us to consider."
Some changes Blizzard is working on involve McCree, D.Va and Widowmaker balance adjustments. Overwatch's competitive mode will also be out later this month.