It varies depending on the country, but in general yes, they're allowed to do so if they need to verify your ticket or they notice you've slept “past” your disembarkation point – I used to live in NYC, and this happened to me on several occasions – or most other “ordinary” reasons.