There are two ways I am aware of to build the fence on a slope: contour or step. In the contour approach the fence follows the topography and each fence post is the same height (5-feet?) above grade. The rails simply follow the contour.

In a step installation, each section of the fence is installed level, but it stair-steps up or down any changes in grade. The uphill post is set to the fence top level (5-ft?) of the section below, and the rails are installed level to the next post, which is set to the same height for the next section downhill. This leaves a gap at the bottom of the fence on the down-hill side of each section. This can be filled with rock, screening or longer fence boards (for a vertical privancy fence) can be used to fill the gap.