There are actually three different types of violations that are 5-second violations. Each of those violations happens at different situations of the game, and truly the only thing they have in common is the 5 seconds.
This 5-second violation is called when a player holds the ball for five seconds and does not shoot, pass, or dribble the ball while being closely guarded by an opposing player. That is not very often committed by players as the game grows more dynamic, players pass and dribble more, and faster than ever. However, if a player is trapped, or very well guarded by the opponent, he can sometimes commit this 5-second violation. When this happens, the ball is turned over to the opponent.
This rule is exclusive to NBA Basketball. A player positioned under the extended free throw line cannot dribble the ball with his side or back to the basket for more than 5 seconds. Curiously, that rule is sometimes called the Charles Barkley rule. The rule was instituted in 1999 by the NBA because of hall-of-fame forward, Charles Barkley. Barkley who was incredibly strong and skilled would turn his back to the opponent, dribble and push his opponent until he found himself in a favorable position to score. That proved to be a problem as he would do that every possession, turning the game into a tedious and repetitive experience. When this is violation is committed, the ball is turned over to the opponent.
This is easily the easiest to see and most called of the 5-second violations. This violation happens when a player fails to inbound the ball within five seconds after receiving it from the referee from any place on the court, in any situation throughout the game. That violation happens when the inbound is very well defended by the opponent. This violation leads to possession change.