Hydrated salts have water within their crystals when the crystals are formed from water; an anhydrous salt is where the crystal has had the water driven out. Water of crystallisation occurs for two reasons. The first, the ion has a strong electric field and it polarises water such that the water tends to pack around the ions. (When the field from a charge polarises another material, there is an attractive force between them, which is why rods charged electrostatically will pick up light particles, like small pieces of paper.) When the salt crystallises, the ions take the water with them, and adopt some structure where even more water might be incorporated. This is the reason that salts like sodium sulphate crystallise with a lot of water of crystallisation.
The second reason is mainly for transition metal ions, where the lone pairs of water molecules form weak dative bonds with the ion, by completing the outer electron shell. This usually changes the energy levels of the d electrons, which is shown by a colour change. An example is the cupric ion, reasonably shown with copper sulphate. Water bonds to it, and the ion goes blue. Stronger dative bonding, such as with ammonia, leads to a more intense blue. The hydrated ions form the crystal, but on heating, the crystal powders and the material goes white.