How to create a system where battery power exist as backup power source
The standard way to do this is with a 'diode OR' circuit. Connect a diode from the power supply and from the battery, with the cathodes connected together and to your target circuit. As long as the battery voltage is lower than the power supply voltage, the battery will only power the circuit when power is off.

If you need to sense whether power is on, put a resistor divider on the power net, before the diode, and sense the divided voltage with your microcontroller.

If your battery voltage is higher than your supply voltage, or may be, there are more complicated alternatives available; look for "Power switch", "Ideal diode" or "Power OR controller" ICs.