It's definitely not perfect, like I can grab a nap and not have it notice sometimes so I assume there are a bunch of heuristics at play that create a "best guess".
That said, how rested I feel does typically line up with the number of hours it shows (regardless of how long I've actually spent in bed), and it has a short description about the quality of sleep like "restorative" or "not enough rem" that further lines up with how I feel.