W = F*D is only valid for a constant force and distance measured in the direction in which the force acts. There is a more general equation relating a variable force to distance and work. Properly, it involves calculus and vectors. Not sure what level you're at.
Ignoring the vector aspect, consider the force advancing a small distance ds, so that the force can be taken as not varying enough to matter. Then add up all these contributions over the total distance moved.