Short answer: Law 19.8 is ambiguous and could be interpreted as permitting either 5 or 6 runs. 5 runs would be the common interpretation, though.
From the Cricket World Cup 2019 Playing Conditions:
Overthrow or wilful act of fielder
If the boundary results from an overthrow or from the wilful act of a
fielder, the runs scored shall be
[...] the allowance for
and the runs completed by the batsmen, together with the run in progress
if they had already crossed at the instant of the throw or act.
- Martin Guptill released the ball before Stokes and Rashid crossed on their second run.
- There was no further contact with a New Zealand fielder before it crossed the boundary.
Preliminary question: when does the overthrow take place?
The overthrow, as the term implies, happens at the instant when the fielder throws the ball. There is no scope for it being extended to an action of the batsman. Therefore the fact that it touched the bat of Stokes has no bearing on any runs scored.
Should the finally completed runs be counted, or only the runs crossed at point of overthrow?
The way the sentence is worded means that either option is a valid interpretation. It does not specify whether at the instant of the throw or act refers to both the runs completed and the run in progress, or just to the run in progress.
Without any further information either way, it would be reasonable to assume that the runs completed means completed before the ball goes dead (as it does in normal cricket scoring) - in this case, before it crosses the boundary.
However, standard practice by umpires - supported by the MCC's e-learning guide (not an authority in itself, but gives an idea of how Laws are usually understood) - is to assume that it means the runs completed at the instant of the throw, plus the runs crossed on at the instant of the throw - hence former international umpire's Simon Taufel's opinion that one extra run should have been scored rather than two.
If Law 19.8 had read as follows:
[...] the runs completed by the batsmen at the instant of the throw or act, together with the run in progress if they had already crossed at that instant
the matter would be unambiguously settled in favour of 5.
If Law 19.8 had read as follows:
[...] the runs completed by the batsmen at the instant of the boundary, together with the run in progress if they had already crossed at the instant of the throw or act
the matter would be unambiguously settled in favour of 6.
As it was, either possibility is a valid interpretation.
However, comments by umpire Dharmasena after the match shows that he and Erasmus were using the common interpretation of the law (that is, 5 runs rather than 6). Dharmesena explained that since the second run had been completed at the time of the ricochet, he and Erasmus assumed "that they had crossed each other at the time of fielder releasing the ball." Geoff Allardice, ICC general manager, confirmed that "they were aware of the law when they made the judgement about whether the batsmen had crossed or not at the time" (and as a side point, also confirmed that "the playing conditions don't allow them to refer such a decision to a third umpire").
The MCC's World Cricket Committee subsequently discussed Law 19.8, concluding that they "felt the Law was clear" but also announcing that the matter would be reviewed by the Laws sub-committee.