How does Windows know it's on battery power?
The laptop contains a battery management chip with SMBus, such as a BQ24721C.
This chip sits at the interconnect with the battery, charger and the system voltage regulator.
It handles detection of the charger and then manages the charging rate via an incorporated DC-DC controller. It also controls various transistors to disable or enable power from or to the battery, or from AC to the system.
These chips have various means to communicate with the rest of the system.
It has IRQs (interrupt requests), analog outputs, a power good signal and SMbus.

If the battery is replaceable, it will also contain some intelligence to measure battery status, and protect it. This uses SMbus or something proprietary. An example is a Coulomb Counter that measures the charge going in and out of the battery (eg: LTC2943)

SMbus is built on I2C; it's the "System Management Bus". All important stuff connects to this. For example it's also available on the PCI connectors.
It's loaded with small devices like fan controllers, temperature and ambient light sensors or simple switches and LEDs, like wi-fi on-off or lid closed.

Then it is up to the operating system drivers to know the layout of this bus, and how the devices work. Some of it is somewhat standardized in ACPI.