They don't, obviously, but what they do is charge when the spot price for electricity is low, and then discharge when it's higher.
While not exclusively the case, this drop in price most typically occurs when you have a higher amount of electricity being generated than being consumed, which is usually because of high amounts of renewables because the unit cost of renewables is essentially 0. For example, the middle of the day at peak solar, or windy nights. Fossil fuel generators are burning a commodity with a substantially higher unit cost, so they will mostly turn off (where possible) during these times.
This is why batteries are often counted under renewable, because they shift an abundance of renewable power in to times when there is less or none.
Personally I don't think we should really be considering the extensive use of rare earth metals as renewable, but that's a separate point.