Due to an unexpected weather event last night my evening commute time was quadrupled, and I had to be back at work only 4 hours after I was able to pull in to my own driveway. It didn't occur to me until today that since I'm usually first one in to work and the last to leave that no one would have been able to tell that I never went home. When is it okay to turn a laptop case in to a pillow and sleep overnight in the office?
Circumstances in which I have overnighted. This is just my personal view, I'd rather have four hours at home than sleeping on my laptop case.
If I had urgent work that HAD to be completed, this has happened a few times in disaster recovery situations.
Cyclone made it unsafe to leave, this happened once.
Too drunk to care, a couple of office parties in my youth left human wreckage strewn around the place until morning.
In your particular scenario, it would be appropriate to ask your boss first and not make a habit of it.