When an ionic compound is dissolved in water, the water molecules surround the ions and form a hydration shell around them. The positive end of the water molecule is attracted to the negative ions, and the negative end of the water molecule is attracted to the positive ions. This hydration shell helps to keep the ions separated and prevents them from recombining.
The strength of the attraction between the water molecules and the ions depends on the charge of the ions. The more highly charged the ions, the stronger the attraction will be. This is why ionic compounds with highly charged ions are more soluble in water than ionic compounds with less highly charged ions.
The polarity of water molecules also allows them to dissolve other polar molecules and compounds. This is why water is a good solvent for many different types of substances.