It was a divine judgement in cases where the evidence was inconclusive and previous attempts to resolve the case had failed. In some cases at least, it was only used after other attempts at a resolution had failed.
We can't be certain why it was considered a valid outcome as there is insufficient evidence, but it is likely that the apparent verdict of a god would have been convincing evidence for believers.
The law referred to in the question is not the only mention of a river ordeal in ancient Mesopotamian law codes. The Code of Hammurabi itself later on refers to this ordeal in cases of suspected adultery (see No. 132). Also, the earlier (c. 2100–2050 BC) Code of Ur-Nammu (No.14) similarly mentions the river ordeal for adultery.
The ordeal was not so much a means of giving evidence as a referral of
the issue to a higher court — that of the gods. Clear examples are found
only in Mesopotamia and Anatolia, where it took the form of a river
ordeal, the river being conceived of as a divinity. The trial could
involve one or both parties. The mechanics are not well documented,
but it seems that ordeals were carefully monitored and could involve
swimming or carrying an object in water a certain distance. At Mari,
the use of substitutes for the parties is attested. Drowning indicated
guilt, but the unsuccessful subject could be rescued prior thereto and
punished. The issue need not be criminal; already in the third
millennium, disputes over property could be settled by ordeal.
Source: Raymon Westbrook (ed), 'A History of Ancient Near Eastern Law' (2003) p.34
According to Bertrand Lafont and Raymond Westbrook in Westbrook (ed), the river ordeal was, in at least some cases, a last resort once other means had been exhausted. After a claim against another person had been made, a request could be made by either party to have access to a court. These included an investigation report, declarations by the parties concerned, and the weighing of evidence and statements.
...The judgment may be a direct decision closing the case ... in favor
of one party. It may also be a decision depending on further proof.
This can be realized by a declaratory oath ... of one of the parties
or of one or more witnesses to the fact or to the original
transaction...
An alternative to the oath is the river ordeal. It seems to have been
practiced quite frequently, as a large list with seventeen short
protocols of ordeals and another fragmentary tablet, both from Nippur,
show....Most disputes are over fields, some over silver, barley, oxen,
and sheep; one is about a slave...
The river ordeal was also used in cases of witchcraft and adultery in the Neo-Sumerian period. In one case concerning a princess, a substitute was used. The best evidence we have on river ordeals comes from Mari in modern-day Syria. Citing J.-M. Durand, Westbrook notes
the procedure there involved swimming a set distance, perhaps with
some handicap, such as carrying a millstone or swimming underwater.
Prior to entering the water, the swimmer reiterated the claim at
issue, sometimes in answer to a series of interrogatories (ARM 26/1
249, 253). The use of substitutes was possible: townspeople swim for
their prince (ARM 26/1 249), a lady-in-waiting for her queen (249), a
wife for her husband (254), a mother for her daughter (253). If the
swimmer failed to complete the course and a fortiori if he drowned,
his case was lost, by divine judgment.
According to Westbrook,
The reasons for using the ordeal are unclear. It had the advantage of
giving an immediate divine judgment, whereas with the oath, divine
punishment would have to be awaited. It may also have been the last
resort when all other attempts to ascertain the truth had failed. In
CT 29 42–43, after the plaintiff had twice rejected a judgment based
on the oath, the king sent the parties to “the river god, the judge of
truth.”
Worth noting is that
the ordeal was undergone freely and voluntarily (e.g., MDP 23 242:1–5
ina †ubàti“u ina nar"amàti“u), but it could still have been ordered by
the court. The subject could refuse to undergo the ordeal, thereby
losing the case.
Thus, the subject's faith was being tested and, undoubtedly, the ordeal was open to manipulation (see Betterthan Kwora's answer for more on this). Whether or not the ordeal was most likely to be undertaken by the innocent, and the outcome manipulated on that basis, we can't say for the time of Hammurabi. Unlike some of the medieval evidence (which shows most trials by ordeal resulted in a 'not guilty' verdict), we do not have enough evidence on verdicts for ancient Mesopotamia. We know that some were found 'not guilty' (the aforementioned princess again) while others were not (there are records of people who failed, some of whom were fished out of the river to be punished).
Whether women undertaking the river ordeal after being accused of adultery were more likely to be guilty or innocent in the first place is a matter of some debate. Siaw Fung Chong, in Innocence by Ordeal or Ordeal By Innocence? observes that, in the ancient Near East,
women facing accusations from their husbands or from the man folks of
their communities—even when there is no evidence—may find it
challenging to defend themselves.
A guilty woman might have little to lose as, in the Code of Ur-Nammu, at least, she would be killed if guilty; the river ordeal could be her only chance of survival.
Allowing the use of substitutes, at first glance, may seem to negate the aspect of faith being tested. However, if someone was able to find to find a substitute (or lots of substitutes in the case of "townspeople swim for their prince") willing to undergo the ordeal on their behalf, it could be seen as a public statement of belief by a third party in the subject's innocence (though whether the aforementioned princess' substitute had much choice is of course debatable).