Because the Hydrogen and Oxygen have a slight positive and negative charges that attract them to each other. As to why they bond in that ratio is because they're balanced that way, their electrons balance their covalent bond. This ratio of two Hydrogen and one Oxygen is well balanced therefore easily formed