The LEGO LiPo battery is rated for 7.4 Volts, I believe. Thus, a single diode would be enough to switch between the 9 Volt rail supply and the LiPO when the rail voltage fails or falls below 7.4 Volts.
Note: If the speed control is DC instead of DCC, then in any case when you set the throttle to slow, the rail voltage will drop below LiPo voltage. This means that below a certain speed setting at the throttle, the loco will continue to run at the speed defined by the LiPo, and will not drop lower - instead, the rail supply will simply not be used until the throttle is turned up again.
This following schematic addresses cases where the rail voltage does not drop below the LiPo voltage.

simulate this circuit – Schematic created using CircuitLab
Why this works:
- When the rail supply from the wall wart is on, the cathode of D1 is at a higher voltage (~8.3V) than the anode (7.4 Volts) - at least until the throttle reduces speed, assuming DC control. Hence D1 acts as an open circuit.
- When the rail voltage is set below the LiPo voltage, or off, D1 conducts in the forward direction, supplying ~6.7 Volts to the voltage controller. D2 becomes open, preventing voltage to the wall-wart from the LiPo.
- Of course, this is not satisfactory if prototypical scale-speed slow running is desired.
If the 0.7 Volt diode drops are unacceptable, use a high current Schottky diode such as the Vishay 95SQ015 instead: 0.25 Volt drop at up to 9 Amperes.
If the intent is to continue using rail voltage and DC speed control until the rails actually are supplying practically no voltage (say down to 2-3 Volts or so), and then switch over to LiPo power, that too can be done - but the loco would then speed up to a fixed speed as determined by the LiPo's supply, until rail voltage came back up to the chosen threshold value.
To achieve this, a Schmitt Trigger takes the voltage from the rails as input, while its supply voltage is from the LiPo cell. The output is used to drive a switching solution, such as a MOSFET, to switch between rail supply and LiPo supply for the motor. The supply would come from the rail until the rail voltage drops below the Schmitt Trigger's threshold voltage that you preset. If this is the solution you are looking for, a schematic can be added if desired.
Edit: Added schematic.
A simple schematic for achieving the objective described in OP's comments:

simulate this circuit
Note that both the LiPo battery positive voltage rail, and the LiPo battery motor output rail, are used in the schematic.
- The rail voltage supplies the motor as long as it is over 0.65 Volts or so, and the P channel MOSFET remains off, so the LiPo supply to the motor is blocked.
- Once the rail voltage drops below around 0.65 Volts, the MOSFET switches on, and the motor is then supplied from the LiPo's motor output line - adjust that output to provide a very slow motor speed, else there will be an abrupt increase in speed the moment the external supply via the rails stops or drops below 0.65 Volts.
- To reduce or eliminate any switching back and forth at the threshold voltage, play with the C1 capacitor's value. In the experimental circuit I've put together, I find that value to give stable switch-over, but YMMV.
Alternatively, more complex logic circuitry needs to be added on board the loco, to distinguish between crawling speed as determined by the controller, and no-power-on-rails, and to lock the no-power voltage from the LiPo to a defined value (i.e. a predecided speed). This however is a pretty strong case for DCC instead of DC. If you are already on DCC, then it's no big deal.