Hamilton was born on 13 December 1730 (or 12 January 1731) in either London or at Park Place, Berkshire, the fourth son of Lord Archibald Hamilton, governor of Jamaica and seventh son of the 3rd Duchess of Hamilton, and Lady Jane Hamilton, daughter of James Hamilton, 6th Earl of Abercorn. His mother was a favourite, and possibly a mistress, of the Prince of Wales and William grew up with his son George III, who would call him his "foster brother". At age nine, he went to Westminster School, where he made lifelong friends of Frederick Hervey and David Murray. Hamilton used to say that he was born with an ancient name and a thousand pounds; as a younger son he would have to make his own way in life. So, six weeks after his sixteenth birthday, he was commissioned into the 3rd Foot Guards as an ensign. He spent some time with the regiment in the Netherlands, and advanced to lieutenant in 1753. In September 1757 he was present as aide-de-camp to General Henry Seymour Conway at the abortive attack on Rochefort. The following year he left the Army, after having married Catherine Barlow, the daughter of Hugh Barlow, Member of Parliament for Pembroke Boroughs. The couple shared a love of music, and the marriage, which lasted until Catherine's death on 25 August 1782, was a happy one. There were no children. When Catherine's father died in 1763 she inherited his estates in Wales and these provided the Hamiltons with a steady income.
In 1761 Hamilton entered Parliament as Member for Midhurst. When he heard that the ambassador to the court of Naples, Sir James Gray, was likely to be promoted to Madrid, Hamilton expressed an interest in the position, and was duly appointed in 1764.
Hamilton arrived in Naples on 17 November 1764 with the official title of Envoy Extraordinary to the Kingdom of the Two Sicilies and would remain as ambassador to the court of Ferdinand and Maria Carolina until 1800, although from November 1798 he was based in Palermo, the court having moved there when Naples was threatened by the French Army. As ambassador, Hamilton was expected to send reports back to the Secretary of State every ten days or so, to promote Britain's commercial interests in Naples, and to keep open house for English travellers to Naples. These official duties left him plenty of time to pursue his interests in art, antiquities, and music, as well as developing new interests in volcanoes and earthquakes. Catherine, who had never enjoyed good health, began to recover in the mild climate of Naples. Their main residence was the Palazzo Sessa, where they hosted official functions and where Hamilton housed his growing collection of paintings and antiquities; they also had a small villa on the seashore at Posillipo (later it would be called Villa Emma), a house at Portici, Villa Angelica, from where he could study Mount Vesuvius, and a house at Caserta near the Royal Palace.
Hamilton began collecting Greek vases and other antiquities as soon as he arrived in Naples, obtaining them from dealers or other collectors, or even opening tombs himself. In 1766–67 he published a volume of engravings of his collection entitled Collection of Etruscan, Greek, and Roman antiquities from the cabinet of the Honble. Wm. Hamilton, His Britannick Maiesty's envoy extraordinary at the Court of Naples. The text was written by d'Hancarville with contributions by Johann Winckelmann. A further three volumes were produced in 1769–76. During his first leave in 1771 Hamilton arranged the sale of his collection to the British Museum for £8,410. Josiah Wedgwood the potter drew inspiration from the reproductions in Hamilton's volumes. During this first leave, in January 1772, Hamilton became a Knight of the Order of the Bath and the following month was elected Fellow of the Society of Antiquaries. In 1777, during his second leave to England, he became a member of the Society of Dilettanti.
When Hamilton returned to England for a third period of leave, in 1783–84, he brought with him a Roman glass vase, which had once belonged to the Barberini family and which later became known as the Portland Vase. Hamilton had bought it from a dealer and sold it to the Duchess of Portland. The cameo work on the vase again served as inspiration to Josiah Wedgwood, this time for his jasperware. The vase was eventually bought by the British Museum. He was elected a Foreign Honorary Member of the American Academy of Arts and Sciences in 1792.