How is the operating system able to know the battery level?
As a complement to the other answer, how does the software running on the computer know what the battery level is? It asks the battery.
Most laptop batteries are smart batteries that have their own microcontroller or "fuel gauge" ASIC, which the host can communicate with over SMBus. People have reverse engineered some examples.
The SMBus may or may not be exposed directly to the operating system in a way that allows the administrator to query it directly. There are various programs like OpenHardwareMonitor or Speccy or lm-sensors that can interrogate the bus to find out about the hardware.
The operating system interfaces with the firmware of an embedded controller that is part of the Advanced Configuration and Power Interface (ACPI).
Wikipedia defines it as :
In a computer, the Advanced Configuration and Power Interface (ACPI) provides an open standard that operating systems can use to discover and configure computer hardware components, to perform power management by (for example) putting unused components to sleep, and to perform status monitoring. First released in December 1996, ACPI aims to replace Advanced Power Management (APM), the MultiProcessor Specification, and the Plug and Play BIOS (PnP) Specification.[1] ACPI brings the power management under the control of the operating system, as opposed to the previous BIOS-centric system that relied on platform-specific firmware to determine power management and configuration policies.
Internally, ACPI advertises the available components and their functions to the operating system kernel using instruction lists ("methods") provided through the system firmware (Unified Extensible Firmware Interface (UEFI) or BIOS), which the kernel parses. ACPI then executes the desired operations (such as the initialization of hardware components) using an embedded minimal virtual machine.
The answer is then that a circuit or micro-chip is embedded in the motherboard, that itself contains a micro operating system which makes available some services via the computer firmware - UEFI or BIOS. It controls many aspects of power and device management.
The computer operating system has a system driver that is dedicated to interfacing with ACPI. Once ACPI is activated, it takes exclusive control of all aspects of power management and device configuration.
In many aspects ACPI is an operating system behind your operating system, except that it comes with the motherboard and is not under your control. There have been voices likening it to a Trojan horse and calling it a security risk. It may be disabled, but some computers may not boot without it, and advanced power management is then in any case disabled as well.
For more information about its use in Windows see the article Battery and power subsystem hardware design.
More generally, all computer chips have documentation which tells designers and programmers what they do and how to configure them to do it. Low level access to these chips can be done by reading and writing directly to the chip's registers.
More complicated chips can come with a software program called a 'driver' that allows high level access to the operating system or even other applications. Your smart phone for example has an Application Programming Interface (API) that allows access to much of the phone's hardware, GPS, accelerometer, battery, camera, etc. When you write an "App", you can access these hardware devices using the API just as the operating system can (though the OS usually has more extensive access than a program running in 'user space'.)
These interfaces are very well defined so that when you make a function call to the hardware chip, it will respond with the information requested. The battery controller chip is just one example of this generic infrastructure.
Whomever writes the operating system code gets the documentation for the chip and writes software to communicate with the chip and retrieve the information it desires.
So the next time you load a 'driver' (or it is done automatically) for an external hard drive, USB thumb drive, or something else, you'll understand a bit more about how things happen 'under the hood'.