When the ball stops being live
The ball becomes dead when the law or umpire says play has ended: for example after it is finally settled, a boundary is scored, a dismissal is completed, or the umpire intervenes for a dead-ball situation.
Byes, leg byes, and dead ball calls sit behind many strange scorecard moments. The umpire has to decide whether the ball was live, how the runs were made, and whether the batter made a genuine attempt required by the law.
The ball becomes dead when the law or umpire says play has ended: for example after it is finally settled, a boundary is scored, a dismissal is completed, or the umpire intervenes for a dead-ball situation.
Byes are extras scored when the ball passes the striker without touching the bat or the striker's person and the batters complete runs or the ball reaches the boundary.
Leg byes can be scored after the ball touches the striker's person, but the umpire may disallow them if the striker neither attempted to play the ball with the bat nor tried to avoid being hit.
If the ball is dead before a later run, overthrow, or dismissal attempt, that later action may not count. This is why the exact moment of dead ball matters.