A lens used in both contacts and eyeglasses works by changing the direction of light and bending it so that it focuses on the retina of the eye. For nearsighted people, a lens "diverges the light rays, which reduces the eye's focusing power," according to Gary Heiting, OD of AllAboutVision.com. This causes the divergent light rays to focus on the retina, instead of before it.
In the case of farsighted people, a lens is used to cause light rays to converge, and move the focus point of the eye forward, onto the retina. Contact lenses and eyeglasses both indicate power through diopter measurements.