Fish, like most animals, are osmoregulators. They regulate their salt intake through their kidneys and their gills. Excess water can be lost through the gills and excess salt can be lost through the kidneys. This way, they can keep the right internal concentration of water and salt, even if the environment has a different concentration.
The ones that can cope with great differences in external salinity are called euryhaline fish. They can live in either type of water as well as in brackish water. Those with a limited ability to control their water and salt intake are called stenohaline fish. They live in an environment that has a specific salinity.
Salmon are an example of a euryhaline osmoregulator. They can cope with a great range of external salinities and still be able to maintain the correct internal concentrations. They hatch in fresh water, swim to the ocean and spend most of their lives there. They then swim back to their spawning grounds (where they hatched) to lay their own eggs. They go from fresh to salt water and back again.
Most fish are stenohaline osmoregulators. They can control their internal ion concentrations but only within limited external ranges. Thus, most freshwater fish die in salt water, and most saltwater fish die in freshwater.