Lilly commenced his military career in the service of the Dukes of Zelle and Hanover in 1685, and was under the command of Prince Frederick Augustus and of Lieutenant-general Chauvet.
He served several campaigns against the Turks in Hungary, and was present at the battle of Grau and the sieges of Neuhausel, Caschaw, Polack, and Buda (1683–6).
In 1693 he was sent with the expedition under Sir Francis Wheler to Barbados, Martinique, the Leeward Islands, New England, and Newfoundland, where besides his post of engineer he had chief command of the artillery train, and was captain of a company of foot.
On 12 October 1694 the Earl of Romney, master-general of the ordnance, appointed him engineer and to command the train of artillery for the West Indies.
He went out with Colonel Lillingston in 1695, and served at the sieges of Cape François and Port à Paix in Hispaniola, which were taken from the French, and he was afterwards stationed at Jamaica.
On 19 May 1696 he was appointed fireworker to the artillery train, and the same year was sent to Cuba to report on the situation and strength of the Havana, after which he returned to England.
In accordance with a warrant of the governor, dated 1 May 1698, Lilly proceeded with the squadron under Admiral Benbow to examine the Spanish ports on the coast of Peru.
He visited Portobello, Carthagena, and the Scottish settlements, &c., and returning to England laid reports upon the capabilities of these ports for defence before the king.
On 4 May 1704 the board of ordnance appointed him chief engineer in the West Indies, and instructed him to fortify the island of Barbados under the orders of General Sir Bevil Granville, the governor.
In the summer of 1711, under a warrant of the board of ordnance dated 6 March, he proceeded to Newfoundland to report on the harbours of St. John and Ferryland, and to settle matters in controversy relating to the security and fortification of those ports.
On the accession of George I, by royal warrant of 2 March 1714–5, Lilly was continued in the post of third engineer of Great Britain, and by a warrant of the board of ordnance, dated 22 March, was appointed to examine the fortifications of Portland, Dartmouth, Plymouth, Falmouth, and the Scilly Islands; and to survey, repair, and project what might be necessary to maintain and improve the defences of those places.
On the accession of George II his appointment as third engineer of Great Britain was renewed by royal warrant of 23 December 1727, and his pay was increased from 150l.
He was so ill that it was reported home by the masters of some ships from Jamaica that he was dead, and he was in consequence struck off the books for salary for March quarter 1730.
He continued, however, to reside in Jamaica, constructing Fort George at Port Antonio and superintending all the other works of defence and barracks in the island.