I've noticed that Picard calls Riker 'Number 1' regularly and I thought nothing of it. But in a recent episode I watched (where there's the simulated battle between the Hathaway and Enterprise-D), Data is the temporary 1st officer and is also called 'Number 1'. I also noticed that Capt. Pike calls his first officer 'Number 1' in the two-parter in TOS where Spock is given a court martial. Nevertheless, Kirk and Janeway don't call their first officers 'Number 1' (AFAIK). I was wondering if there was any documentation/reference anywhere to this being something of a captain's personal choice, or if it is actually an 'official title' (albeit affectionate) for a first officer?
It goes back to the days of sail - First Lieutenant back then was not a rank, but a position - he was the "first" of the ship's officers below the Captain (depending on the size of ship and crew, there would be Second and Third Lieutenants). Since the Royal Navy is steeped in tradition, executive officers continued be nicknamed "number one", even as Second and Third Lieutenants unused.
"Number one" is not an official title - and wouldn't be recorded in logs or communications - but it is a traditional one. It is also useful to isolate the second-in-command with the title - particularly if there are other officers holding the same rank on the ship (for similar reasons, the Chief Engineer and Chief Medical Officer are called by their title).
An Executive Officer (XO) is a position in both the US and British armed services. On ships the XO is the second in command of a vessel.
In the British Royal Navy, on smaller vessels such as submarines and frigates, the XO is typically a First Lieutenant - and is often informally called the 'number one', though apparently calling them 'the XO' is becomming more common.
So it seems that the styling of 'number one' for the second in command of a vessel does have some basis in real military terms.'
You can read more about the Executive Officer position on wikipedia.
According to Memory Alpha, the term derives from the fact that the Captain's most senior office is called the First Officer of the ship, and is also known as the XO.