To add to the other answer here re: Linux, Windows software can also access this information through various provided WinAPI methods/objects. One such example is Win32_ComputerSystem, which among others, has members such as:
PCSystemTypeEx
Data type: uint16
Access type: Read-only
Type of the computer in use, such as laptop, desktop, or Tablet.
Possible values include:
Unspecified (0)
Desktop (1)
Mobile (2)
Workstation (3)
Enterprise Server (4)
SOHO Server (5)
Appliance PC (6)
Performance Server (7)
Slate (8)
Maximum (9)
As for how Windows knows this, while I can't say with certainty because I lack both the Windows source code and any official documentation links presently, I'd say its a simple matter of the hardware ID's within the computer giving this information away to the OS. Since Windows has a massive partner network that submit their drivers directly to Microsoft (for driver updates via Windows Update, "standard" drivers to include with install media), it's pretty easy for Windows to figure out precisely what kind of computer you've installed it on. Just by the CPU ID alone you could make a pretty safe bet whether the machine is a laptop or desktop.