Simply put, a battery generates electrical energy from chemical reactions. It stores this chemical energy till it is delivered as electrical power, when connected to a device.
A battery consists of one or more electrochemical cells (convert chemical energy to electrical energy). An electrochemical cell consists of a negative electrode, electrolyte and a positive electrode. The electrode is where the pertinent electrochemical reactions occur, and an electrolyte is the barrier between the electrodes. It allows the selective diffusion of ions and is impermeable to electrons (electricity is succinctly the flow of electrons).
So how does that work?
When a battery is connected to an electrical device, two reactions happen simultaneously at the electrodes. At the positive electrode (called anode), a chemical reaction occurs with ions from the electrolyte, that releases electrons. Since the electrolyte is impermeable to electrons, the easiest path of flow for the electrons is through the connected external load, powering the device. The ions however, flow through the electrolyte to the negative electrode (called cathode). Here, another chemical reaction occurs at the cathode, consuming the electrons and ions, and completing the circuit. Simply put, the anode releases electrons (process is called oxidation), and the cathode consumes them (process is called reduction). Check out my Fuel Cell article, to see how similar in operation batteries and fuel cells are!
As the chemicals in the battery anode/cathode produce or consume electrons, they are converted into another chemical, hence, consumed in the process. Consequently, power generation is limited by the mass of material present in the electrode that can produce electrons and ions, to power the connected device. As with every battery, once the battery components are consumed, power generation ceases. This mode of battery operation is called Discharge.
However, certain batteries like those in your cell phone or car, can be recharged. This mode of battery operation is a reverse of the discharge mode. Recharging works by applying a power source, the reverse potential drives the reactions that generated power during discharge in the opposite direction. A popular rechargeable battery is the Li-ion battery. We will use this system to demonstrate discharge and recharge mechanisms in a battery:
In discharge mode, Li ions and electrons are generated in the positive electrode. The electrons generated flow out of the positive electrode (when connected to an external load), to power the connected device. The Li ions flow through the electrolyte to the negative electrode. Here, the Li ions, graphite (C6), and the electrons (coming full circle after powering the connected device), react to form LiC6.
In the recharge mode, a voltage source is connected across the battery. This drives the LiC6 to form graphite (C6), Li ions and electrons. The electrons are driven to the positive electrode, where the electrons, ions and cobalt oxide (CoO2) react to form lithium cobalt oxide (LiCoO2), completing the circuit. This replenishes the starting chemical (lithium cobalt oxide (LiCoO2)) needed to take up another discharge mode.
The specifics of these reactions in batteries differ depending on the chemical composition of the battery, however, the basic principle of operation of all batteries is the same!