The incident occurred in the 52nd over of the England innings with Bairstow and Ben Stokes rebuilding for England after the dismissal of Ben Duckett.
Bairstow left the final ball of Cameron Green's over alone to the wicketkeeper and started walking outside the crease assuming the ball was dead. However, an alert Carey realised there was an opportunity to run the batsman out and effected a directed hit at the striker's end to catch Bairstow well short.
According to law 20.1.2 of the MCC's Laws of Cricket, "the ball shall be considered to be dead when it is clear to the bowler’s end umpire that the fielding side and both batsmen at the wicket have ceased to regard it as in play."
Carey, having watched Bairstow make it a habit of walking outside the crease early, used the chance to run the batsman out.
In this case, the fielding side clearly considered the ball to still be in play and hence it wasn't a dead ball.