You may leave the game before it ends, but you will get punished. Your next 1-5 games will be in a 'Low Priority' form. In low priority, every player gets a pool of only 3 heroes to pick. They can also not play with 'High Priority' players.