Water law is complicated and detail intense, but the basic principle is "prior appropriation".
In other words, you gain a water right by taking water from a river or other body of water and appropriating it for your use on a particular date, and then continuing to do so going forward. The older the date at which you first started using water in a river basin, the higher priority your water right will be.
State officials (including a corps of civil engineers) and the courts with judges trained in water law, engage in the complicated matter of determining who has water rights from particular dates in particular river basins, in determining which prior appropriation date will provide the cutoff date for a particular snowmelt season, and in punishing people who use water from a river basin that their priority is insufficient to allow them to use.
Once established, water rights may be purchased and sold like property. So, for example, if a farming has water rights to X units of water dating with a 1891 appropriation date, and a developer wants to build a new subdivision whose residents need water in the same river basins, the developer can purchase the X units of water with an 1891 appropriate date from the farmer (who goes out of business without these water rights) and can give it to an HOA or water district for the developer's new subdivision.
There are special rules that tweak this general principle. The Western states have interstate compacts, which are basically treaties between U.S. states, that govern how much water upstream states must allow to pass to downstream states. These are not infrequently litigated in the original jurisdiction of the U.S. Supreme Court.
There are also special rules governing when and how much precipitation one can store in a cistern or rain barrel, treaties with Indian tribes regarding water rights, rules on storage of water you are allowed to use with your water rights, rules about wasting water, and rules about how groundwater aquifers are handled.