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 the his first leave in 1771 Hamilton arranged the sale of his collection to the British Museum for £8,400. 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.
In 1798, as Hamilton was about to leave Naples, he packed up his art collection and a second vase collection and sent them back to England. A small part of the second vase collection went down with HMS Colossus off the Scilly Isles. The surviving part of the second collection was catalogued for sale at auction at Christie's when at the eleventh hour Thomas Hope stepped in and purchased the collection of mostly South Italian vases.
Soon after Hamilton arrived in Naples, Mount Vesuvius began to show signs of activity and in the summer of 1766 he sent an account of an eruption, together with drawings and samples of salts and sulphurs, to the Royal Society in London. On the strength of this paper he was elected a Fellow of the Royal Society. In the autumn of 1767 there was an even greater eruption and again Hamilton sent a report to the Royal Society. The two papers were published as an article in the Society's journal Philosophical Transactions.
The Royal Society awarded him the Copley Medal in 1770 for his paper, "An Account of a Journey to Mount Etna". In 1772 he published his writings on both volcanoes in a volume called Observations on Mount Vesuvius, Mount Etna, and other volcanos. This was followed in 1776 by a collection of his letters on volcanoes, entitled Campi Phlegraei (Flaming fields, the name given by the Ancients to the area around Naples). The volume was illustrated by Pietro Fabris.
Hamilton was also interested in earthquakes. He visited Calabria and Messina after the earthquake of 1783 and wrote a paper for the Royal Society.
The Hamiltons gave musical entertainments in which they played (William the violin, and Catherine the harpsichord or pianoforte) with some of their servants or professional musicians. During their tour of Italy in 1770, the young Mozart and his father were introduced to the Hamiltons. Catherine played the harpsichord for Mozart, and he said he found her performance "uncommonly moving". Another musical visitor was the music historian Charles Burney, who stayed at Villa Angelica at Portici and was helped by Hamilton with his research. Burney thought that Catherine Hamilton was the best harpsichord player in Naples. When the young Irish singer Michael Kelly arrived in Naples, he was introduced to the Court by Hamilton. The young Irish composer Thomas Carter also stayed with him around 1788.
Catherine Hamilton died in August 1782; her body was returned to Britain for burial in the Barlow vault at the old Slebech parish church. Hamilton wrote to his niece Mary: "I must for ever feel the loss of the most amiable the most gentle and virtuous companion that ever man was blessed with".
A year after his wife's death Hamilton had a third period of leave in Britain. This time he visited his estates in Wales and went on a trip to Scotland with his nephew Charles Greville, who was a younger son of the Earl of Warwick and Hamilton's sister Elizabeth. When Hamilton made a will during that visit to Britain he made Greville his heir. In London, Hamilton met Emma Hart who at that time was Greville's mistress.
On his return to Naples, Hamilton was lonely. "After having lived 22 years en famille it is most terrible to live chiefly alone", he wrote to his niece. Meanwhile, Greville, who wanted to search for a wealthy wife, was hatching a plan to persuade his uncle to take Emma off his hands. Somewhat reluctantly Hamilton agreed to the plan and Emma arrived in Naples on her 21st birthday, 26 April 1786. With her was her mother, who went by the name of Mrs Cadogan, and who would live with Emma and Hamilton until she died in 1810. The women were installed in an apartment in the Palazzo Sessa, and provided with a carriage and wardrobes. Tutors were hired to give Emma lessons in Italian and singing. It was some months though before Emma accepted the fact that Greville was not going to join her in Naples or send for her to go back to England. At that point she decided to become Hamilton's mistress.
Hamilton and Emma married five years later, when Hamilton obtained leave of three months in Britain. As he held a public position he had to ask the King's permission. The ceremony was a quiet one in St Marylebone parish church, taking place on 6 September 1791, two days before the couple's return to Naples. Emma signed the register as Amy Lyons; the witnesses were Hamilton's cousin the Marquess of Abercorn and Louis Dutens, who had been chaplain at Turin. The following day Hamilton was sworn of the Privy Council in 1791.
For most of Hamilton's time as ambassador, Naples had been a political backwater. But when France declared war on Britain in 1793 events in Naples became more turbulent, and Hamilton's role became more important, just as his health was declining. Nelson's fleet arrived in the Bay of Naples after defeating the French Fleet at the Battle of the Nile in August 1798 and Nelson was a guest of the Hamiltons. At the end of the year the King and Queen abandoned Naples as the French Army advanced and fled to Palermo in Sicily. The Hamiltons went with them.
In the summer of 1799 Naples was recaptured from the French and savage reprisals were enacted against those who had supported the Parthenopean Republic. Hamilton, together with the king and queen, and Nelson, remained in Palermo, except for a visit to the Bay of Naples in Foudroyant when he briefly went ashore. Hamilton was anxious to return to Britain. At the beginning of 1800 Sir Arthur Paget was sent out to replace him and the Hamiltons and Nelson returned overland to England. By this time Emma and Nelson were lovers. They landed at Great Yarmouth on 31 October 1800. The relationship between Hamilton, Emma and Nelson was already causing a scandal and Hamilton was caricatured in cartoons by James Gillray.
Hamilton would live in retirement for another two-and-a-half years, at Merton with Emma, Nelson, and Mrs Cadogan, and in a house he leased in Picadilly. He spent his time fishing on the River Thames, visiting his estates in Wales, selling paintings and vases, trying to get money owed to him by the government for his expenses in Naples, and attending his clubs, especially the Royal Society and the Dilettanti. He died on 6 April 1803 at his house in Picadilly. He left Emma £800 a year, which included £100 a year for her mother, and small annuities for four servants in Naples. His estates in Wales, which were heavily mortgaged, were left to Greville. Nelson was left an enamel copy of a portrait of Emma by Elisabeth Vigee le Brun.
Hamilton was buried beside his first wife at Slebech.
Hamilton's life was fictionalised by Susan Sontag in her novel The Volcano Lover: A Romance.
In the 1941 movie That Hamilton Woman, the role of Sir William Hamilton was portrayed by Alan Mowbray.
In the Dessau-Worlitz Garden Realm in Germany there is an island in a lake atop which is a model of Mount Vesuvius. Duke Leopold III of Anhalt-Dessau would stage fireworks that seemed to issue from an erupting volcano to entertain his guests. At the foot of the mountain on the island is a building intended to suggest Hamilton's villa at Posillipo.
Walton Ford's watercolour Jack on his Deathbed portrays the death of Hamilton's pet monkey in 1780.