Taking one's hat off in the presence of the soverign is of course a very ancient tradition, and in modern times- at least when peop[le still wore hats- it has been customary to doff one's hat as a sign of respect.
As Mje says above, removing one's hat has long been a sign of respect. As for the phrase "Hats off to...!" - which is what I presume you are asking about - that has been around only since the 1920s, as a sort of order to show that respect. You see it in such sentences as: 'Hats off to the English rugby team!'...'Hats off to the Black Watch!'...and so on. Strangely enough, the very earliest recorded version, which appeared in 'The Daily Mail' in 1923, said: "Hats off to France!"
i agree totally, it was a greeting used in britain for about a century, usually from a gentleman to a lady, Naval vessels use it when passing a ship of another nation. the armed forces also use it when they 'pass out' as a sign of congratulations or 'Bravo Zulu'