
Work is an integral of a dot product of two vectors. The dot product only counts the amount of force, that is applied in the direction of the displacement. In the case of gravity, it acts vertically only. The only displacement that matters, is the vertical part of the movement, which is given by measuring the height of the stairs.