Flying a false flag is when a vessel is flying an ensign from a country they are not registered in an attempt to deceive another, or hide their true identity.
In the Napoleonic wars and the general age-of-sails ships weren't destroyed and sunk but rather taken over by the different navies fighting each other, or any privateer or pirate. So you couldn't judge the nationality of the ship by the design or paint scheme, if the navy in question even had a uniform paint scheme. The ensign on the stern, was the single identifier showing on which side the ship was fighting on.
Raising an ensign of an opponent or neutral nation, was a valid and legal strategy to gain an edge on an opponent. There was however a gentlemen's agreement that the correct flag would be flown as soon as a battle was started. This could be done by changing flags just seconds before starting firing.
In modern times the term False Flag is used for any hostile operation where one side tries to hide their identity by faking to be someone else. Weather this happens at sea or not. It is not much in use on navy ships anymore, as the amount of ships a navy has compared to the age-of-sail era, is much much lower. And there is no stealing and using each others ships anymore. Enemy ships are rather sunk. Also ships are now identified by their design, and even which shade of grey they are painted, rather then their flag.
Merchant ships feature a false flag much more often, if they want to move cargo that is sanctioned by some country they pass, go to or come from. Or if their official flag state is of a questionable reputation. For merchant ships it is a bit more difficult to do, as they are strictly registered. But not impossible.
The pirates from on the Sunset Dawn are changing ensigns constantly. They don't have the name painted on the hull, nor their official registration number that they should have. They get away with this by posing to be a rich super yacht, where the owner pretends to be above the law. Many Harbourmasters and coastguards don't want to take that confrontation, scared that the potential owner knows their boss's boss.