There are several ways of doing it and usually the router uses a combination of 1 to 3 of the more simple variants. The router/AP usually doesn't have the CPU horsepower to do something exhaustive here. Just too much work.
Anyway: It is not guaranteed to work. Some devices don't reveal much information, it may be ambiguous and some devices may actually lie about what sort of device they are.
Typical methods are:
- For Apple devices and many phones/tablets you can tell from the vendor-id in the mac-address. The router simply has a map-table that matches known vendor-Ids to the corresponding OS.
- Many devices respond in a known way to a Bonjour, PNRP, UPNP or NetBios request. Simply send a few of these probes and analyze the resulting answers. From this you can often determine whether it is Windows (and which version), OSX, Linux, a printer (and which model), multimedia-device, TV, etc.
- Devices that support SNMP will often report their OS, model, version, etc. in response to a SNMP query. It takes very little effort to request this info from the device.
- In a number of cases the way a device issues a DHCP request to the router reveals info about the OS.
You mentioned "User Agent". I presume you mean browser user-agent string with that. This is very unlikely as this would require a lot of effort to implement (router must monitor all HTTP traffic for presence of user-agent strings, which takes a lot of effort) and only is usefull for clients that a) have a webbrowser and b) where the user hasn't modified the agent string in something unusual and c) where the user uses a standard browser and not something obscure and d) where the user isn't using "private mode" browsing (which usually doesn't reveal the normal user-agent string).
Lot's of effort for very little reliable information.