Osmosis is the process by which dissolved substances pass from a high concentration area of water to a low concentration, achieving a balanced distribution.
Because a fish's body is semi-permeable (water can be absorbed, lost, and passed through the body), it is subject to the effects of osmosis.
A fish's body contains many substances vital for proper health and function, at higher or lower levels than the surrounding water and will naturally lose or gain substances due to osmosis. Osmoregulation is the control of this process by the fish and a large proportion of the osmoregulatory system takes place in the gills, since this is the area where the membrane between the fishes blood and the surrounding water is thinnest (so more osmosis takes place)
A fishes osmoregulatory system is part of the reason why some fish do best in hard water, whilst others prefer soft water. A fish originating from soft water may be particularly well adapted to retain substances because the surrounding soft water contains lower concentrations of those substances (so they are easily lost through osmosis)
In contrast, a fish originating from hard water may be adapted to reduce the concentration of substances in its body, since the surrounding hard water has too many of those substances. If placed in the wrong type of water, a fish may have difficulty in retaining or loosing substances, causing stress and ill health.