Players are allowed to change during the course of play provided that they are within 5 feet of the bench or that they don't interfere with the play prior to exiting the ice. It's a discretionary penalty that is up to the on-ice official. If there is no prior penalty this would be more than the standard 5 players on the ice.
If in the course of making a change, either the player entering the game or the player retiring from the ice touches the puck with a stick, skates or hands or checks an opposing player it's considered 'too many men'
The penalty is 2 minutes.
A tough call for a team, it's usually attributed to an over zealous player wanting to get onto the ice or a player coming off the ice changing their mind while skating towards the bench (and staying on the ice while the exchanging player goes onto the ice).