In most situations, it's the law of the country where the aircraft is registered that applies on board.
Thus a United States passenger aged 18 travelling aboard a Qantas aircraft in US airspace can legally drink alcohol, even though US law stipulates a minimum drinking age of 21.
However, there are anomalies. A passenger found to be in possession of illegal drugs on a Singapore Airlines flight from Singapore to Frankfurt would be prosecuted under German law, not Singapore's.
Similarly, if an offence happens while the aircraft is on the ground, local laws override the laws of the country of registration.
That's what happened to Pakistan International Airlines' Captain Irfan Faiz, who was found to be three times over the United Kingdom's legal blood-alcohol limit for pilots before he took off from Leeds Airport.
Even though his aircraft was registered in Pakistan, and he would have been permitted to fly under that country's aviation laws since he was not in violation of their 12 hour "bottle-to-throttle" requirement for pilots, he was found guilty under British law and spent 11 months as a guest of Her Majesty.