The upper stage of the Apollo lunar landing module ("LM") is itself a complete rocket ship, with an engine, propellant tanks, small thrusters to steer with, a guidance computer, and all the other requirements.
With the moon's low gravity, it's possible to reach orbit with such a small (~4.5 metric ton) ship, especially as it only needs to operate independently for a few hours. The bulk of the batteries, oxygen, water, and other consumables in the Apollo LM were kept in the descent stage, as detailed here.
The command/service module (CSM) would be in orbit around the moon traveling at a speed of about 1600 m/s. The LM ascent stage would lift off vertically, then tilt over to gain horizontal speed. The liftoff would be timed to reach orbital speed at a point in space reasonably close to the CSM. Once in orbit around the moon, the LM would use its radar and its small thrusters to meet the command module, as detailed in this answer.