Technically, the flagship is the ship used by the comanding officer of a group of naval ships. For example, LT Bulkeley's 77' Elco PT boat was the "flagship" of PT Ron3 - since Bulkeley was the Ron3 CO.
For military ships in harbor, one is designated as SOPA (Senior Officer Present Afloat). SOPA is determined by the seniority of the Commanding Officers of the ships in port, based on their signal number. SOPA flies the "starboard" (a green & white) pennant from the yardarm.
The "flag officers" of most navies are: Commodore (1 star); Rear Admiral (2 star); Vice Admiral (3 star); Admiral (4 star); Fleet Admiral (5 star). Shortly after WWII, the USN, USCG, and USPHS changed their 1 star rank to Rear Admiral, Lower Half; 2 star became Rear Admiral, Upper Half.
Commodore is still used in many navies to designate the officer in command of the flotilla or task force/group if they are not a flag officer.
In the old days of sail, the most senior admiral was usually on the largest and most powerful ship in the fleet, with lesser ranked admirals on smaller ship. During the 20th century, most ships became large enough that they could accomdate the fleet commander and his staff. During WWII most admirals preferred a faster ship (heavy cruiser) over the largest one (battleship).
There are many other reasons for an Admiral to break his flag on a ship, other than being in command of the fleet or task force. The Admiral may be the District, Area, or Fleet Commander presiding over the change of command of a ship under his command. The Admiral may be visiting the ship for some other reason, like an inspection or handing out medals to the crew. When I was serving aboard USCGC VIGOROUS, we embarked the USCG Officer Ancient Marnier at the seabuoy off New London, CT. The Ancient Mariner happened to be a 2 star Admiral, and he was retiring. The transfer of the "Ancient Mariner" title was to be made to another USCG officer at the USCG Academy during the Admiral's retirement ceremony. The USCG Ancient Mariner is a ceremonial title given to the officer and enlisted person with the earliest qualification date as a "Cutterman".
Another term to think about is "Queen of the Fleet". The Queen of the Fleet usually the oldest ship still in active service; which generally rules out the USS CONSTITUTION or HMS VICTORY, as they are more of a museum ship. In the USCG, the Queen of the Fleet has gold hull numbers, and is currently the USCGC ACUSHNET.