The first thing to do is to sepcify 3 components in the 'battery' field of your robot node:
this field should contain three values: the first one corresponds to the present energy level of the robot in Joules (J), the second is the maximum energy the robot can hold in Joules, and the third is the energy recharge speed in Watts ([W]=[J]/[s])
https://cyberbotics.com/doc/reference/robot#field-summary
Then from the controller, you should use the robot/battery API to enable the sensors and retrieve the battery state:
These functions allow you to measure the present energy level of the robot battery. First, it is necessary to enable battery sensor measurements by calling the wb_robot_battery_sensor_enable function. The sampling_period parameter is expressed in milliseconds and defines how frequently measurements are performed. After the battery sensor is enabled a value can be read from it by calling the wb_robot_battery_sensor_get_value function. The returned value corresponds to the present energy level of the battery expressed in Joules (J), if the battery field is empty, this function will return -1.0.
https://cyberbotics.com/doc/reference/robot#wb_robot_battery_sensor_enable
You can find an example of battery simulation available directly by default in Webots:
https://www.cyberbotics.com/doc/guide/samples-devices#battery-wbt