The position of honour on a ship is the quarterdeck at the stern of the ship, and thus ensigns are traditionally flown either from an ensign staff at the ship's stern, or from a gaff rigged over the stern.
The usual rule that no flag should be flown higher than the national flag does not apply on board a ship: a flag flown at the stern is always in a superior position to a flag flown elsewhere on the ship, even if the latter is higher up.
The priority of hoisting locations depends on the rig of the vessel. With sloops, ketches and schooners the starboard yardarm or spreader of the highest or main mast is the second most honoured position. (That is, after the ensign at the stern.)
Next after the starboard spreader is the port spreader.
House flags (those defining the owner) are usually flown from the mainmast truck. When a club burgee is flown, it will normally be hoisted to the truck of the most forward mast. On a sloop, then, not having a foremast, the house flag could be moved to the port spreader if the starboard spreader was in use, and a burgee was being flown. On a ketch, the house flag would be moved to the mizzen.
When in port, the ensign should always be flown from the staff at the stern. This is traditional, because in former times the gaff was then lowered along with the mizzen sail. The only ensign ever flown from the starboard spreader or yardarm is that of a nation being visited. This is known as a courtesy hoisting of a courtesy Flag.
At sea, it used to be that the ensign was flown from the mizzen gaff. When Bermudian sails came into general use, some skippers started to fly the ensign from two-thirds the way up the main-sail leech. Many consider this an affectation with the past. Others have taken to flying the ensign from a backstay. These are not good locations because the flag does not fly out well when hoisted raked forward.
Another recent custom has been to fly a burgee and/or a cruising or power squadron flag from the starboard spreader. This custom has arisen because many sailboats today place a racing flag or wind indicator at the masthead.
Motor boats without masts should always fly the ensign from an ensign staff at the stern. Conventionally, courtesy flags are flown from the jackstaff at the bow. This seems to some landsmen as being a reversal of priorities. However, a boat is steered by the stern and this gives it pride of place.
Nautical etiquette requires that merchant vessels dip their ensigns in salute to passing warships, which acknowledge the salute by dipping their ensigns in return.
Merchant vessels traditionally fly the ensign of the nation in whose territorial waters they are sailing at the starboard yard-arm. This is known as a courtesy flag, as for yachts.