Gravity keeps a plane from flying off into space.
When Lift and Weight are equal, which is the case during the cruise portion of an aircraft's flight, the aircraft maintains a constant altitude. Minor control inputs from the pilot or auto-pilot will ensure the plane stays at altitude. If no input was made by either the plane would wander up and down gently but due to designed aerodynamic stability it will always return, on its own, to about the same airspeed and altitude.
As I said earlier, it is physically not possible for a plane to continue in a straight line off into space. The atmosphere becomes too thin above about 60,000' for engines and wings to function.