Traditionally, a courtesy flag would be flown from the truck of the foremast, the truck being the highest point on the mast as this would invariably be the highest point on a ship - the higher the flag the greater the sign of respect. The company house flag would be flown at the truck of the mainmast with the ensign at the gaff (when underway) or from the stern (when alongside/in port).
The one notable exception to this was Brocklebank Line, they always flew their house flag from the foremast and as far as I'm aware was the only company to do so.
Because of design changes over the years and for convenience, most ships fly all their flags from the mainmast instead. Flags on yards have a seniority system; starboard outer, port outer, starboard inner, port inner etc. Ergo you'll normally see the courtesy flag on the starboard outer yard and the company house flag on the port outer yard.
Whilst it is known as a courtesy, in many countries it is actually a legal requirement to fly a courtesy flag - non compliance can result in a fine or worse.
In Saudi Arabia it is a legal requirement for the Saudi flag to be flown from the Foremast day and night, and at night it should be illuminated. Failure to do this will often see a visit from local officials followed by the arrest of the Master until a fine is paid.