Dynamics of tip-substrate interactions in atomic force microscopy