You may surprised by its rapidity, but when a commercial airplane takes off, and as soon as its wheels are no longer in contact with the runway, the pilot immediately retracts the undercarriage.
If you don’t know why, apart from being a matter of aerodynamics of course, by retracting the undercarriage, the aircraft reduces its drag, consumes less fuel and can even fly faster. In addition, quickly retracting the aircraft landing gear is also for safety reasons.
In the event of an engine failure on take-off, and once the aircraft has exceeded "V1", the speed up to which an airplane can still abort, retracting the landing gear facilitates its ascent. The crew can then gain altitude and attempt to restart the failed engine, or make an emergency landing, after dumping (jettisoning) a certain amount of fuel.
However, just remember that an airliner engine failure is extremely rare, and also when it happens in flight on a twin-engined jet, the ‘plane is capable of reaching a diversion airport. In fact the ETOPS norms for twin-engined planes fixes the maximum distance allowed, in minutes, from an emergency airstrip.