To understand the origin of water line emission and absorption during high-mass star formation, we decompose high-resolution Herschel-HIFI line spectra toward 19 high-mass star-forming regions into three distinct physical components. Protostellar envelopes are usually seen as narrow absorptions or emissions in the H2O 1113 and 1669 GHz ground-state lines, the H2O 987 GHz excited-state line, and the H2O-18 1102 GHz ground-state line. Broader features due to outflows are usually seen in absorption in the H2O 1113 and 1669 GHz lines, in 987 GHz emission, and not seen in H2O-18, indicating a low column density and a high excitation temperature. The H2O 1113 and 1669 GHz spectra show narrow absorptions by foreground clouds along the line of sight, which have a low column density and a low excitation temperature, although their H2O ortho/para ratios are close to 3. The intensities of the H2O 1113 and 1669 GHz lines do not show significant trends with luminosity, mass, or age. In contrast, the 987 GHz line flux increases with luminosity and the H2O-18 line flux decreases with mass. Furthermore, appearance of the envelope in absorption in the 987 GHz and H2O-18 lines seems to be a sign of an early evolutionary stage. We conclude that the ground state transitions of H2O trace the outer parts of the envelopes, so that the effects of star formation are mostly noticeable in the outflow wings. These lines are heavily affected by absorption, so that line ratios of H2O involving the ground states must be treated with caution. The average H2O abundance in high-mass protostellar envelopes does not change much with time. The 987 GHz line appears to be a good tracer of the mean weighted dust temperature of the source, which may explain why it is readily seen in distant galaxies.