By most accounts, the Iberian Religious War between Spanish kingdoms and the Moors was the longest ever, from 711-1492 CE (781 years)
This is quite a complicated question, since there’s a fine line between a war and a series of conflicts/battles that take place over a few hundred years. Historically, it wasn’t until relatively recent years that countries used formal declarations of war or peace treaties.
However, most historians would give the title of longest war in history to the Iberian Religious War (also known as the Reconquista, 711-1492 CE) between Christian kingdoms of modern-day Spain and Muslim Moors. In the 8th century, the Moors were advancing into Europe and claiming territory, and Spain wanted to push them out. As the war continued, Spain was united under Catholicism, and the Reconquista became somewhat of a holy war (around the same time as the Crusades).
The true beginning of the Reconquista in full force was marked by the Battle of Covadonga in 718 when the Christian King Pelayo of the Visigoths defeated the advancing Muslim army in Alcama. Over the next several centuries, a series of battles were fought between the Christians and the Moors, with victories and losses on both sides. In the latter years of the Reconquista, the Catholic Church recognized the war as a ‘holy war’ similar to the Crusades, and several military orders of the Church also participated in the war. Finally, by the 1400’s, the Moors had only a few territories remaining under their rule.
The war ended in 1492 when the Spanish kingdoms recaptured Grenada, the Moors last major stronghold, and succeeded in driving them out of Europe.
Aside from the Iberian Religious War, there are many other wars that could be considered the longest ever. One of these was the 335 Years’ War, between the Netherlands and the Isles of Scilly (near England).
During the English Civil War, the Dutch joined the fight against the English Royalists, eventually prevailing and driving their navy back to the Isles of Scilly. A Dutch admiral blockaded the isles, and declared war on them in 1651. Three months later, the Royalists surrendered, but no peace treaty was signed between the Netherlands and the Isles of Scilly themselves.
It’s debatable whether the admiral had the authority to declare war on behalf of the Netherlands. Regardless, a peace treaty was eventually signed in 1986, despite the fact that there had been no fighting for hundreds of years.
In quite a similar scenario, Rome conquered the nation of Carthage in 146 BCE, but Carthage (by virtue of being burned and destroyed) could not sign a peace treaty. In 1985, the mayors of Rome and Carthage (both now cities) signed an official peace treaty. However, most historians still agree that the war ended in 146 BCE.
There’s also the Hundred Years’ War between England and France, which ironically lasted for 116 years.