The first that springs to mind is the battle of Asculum in 279BC in which Pyrrhus of Epirus beat the Romans but lost so many men that it contributed to his ultimate defeat in the war. Although the actual loss of the war involved a host of other factors and occurred several years later. So even though it may be a famouse example, it might not be the best or most clear cut example.
This is also where we get the term Pyrrhic victory, denoting a costly military victory.
Edit: I thought of several other examples; The Tet Offensive in Vietnam was an assault by North Vietnamese and Viet Cong forces across the cities and regions of Vietnam controlled by the US and its allies. The assault was initially successful for the Communist forces however the US eventually regained the upper hand and inflicted a lot of casualties on the North Vietnamese/ Viet Cong. However although the US won the military victory the general consensus is that the initial losses they suffered lost the hearts and minds of people back home and thus support for the war, resulting in their eventual withdrawal from Vietnam.
As /u/InTheCrosshairs says below, both sides claimed victory making this case as unclear as the rest of the them I guess. I'd recommend seeing his full answer below/above in the thread for a much more comprehensive picture of the Tet Offensive
A slightly alternate take on the question but Hannibal Barca's Italian campaign had many military successes however his Carthaginian forces never took out Rome itself allowing the Romans to rebuild their armies and invade Carthage itself resulting in Hannibal returning to defend his homeland where he was defeated by Scipio Africanus at the Battle of Zama. In this case it was winning a bunch of battles but not acting on those victories to ensure victory in the war, so slightly different to your question. Debatable as to whether that was Hannibal's fault or not, but that's a different discussion.
The Tet Offensive as a whole. (Vietnam War, sorry I have yet to post enough answers to be flaired) :D!
The American motif from the battle of Ia Drang (the movie/book "We Were Soldiers Once..... And Young" for reference) until Tet and beyond was to fight a "war of attrition" against the VC. In other words, we were going to kill more of them than they killed of us. During the Tet Offensive the United States achieved this with stunning precision.
At the battle at the Khe Sanh airbase, the United States Marines from January to July 1968 lost around 400 Marines inside and around Khe Sanh, and while killing an estimated 5,000 to 10,000 Viet Cong. Both sides claimed victory, as the Americans achieved the war of attrition goal while the Viet Cong forced the Marines to destroy the airbase and retreat.
At Hue, the combined force of Americans and ARVN troops lost around 600 troops, while the PAVN lost an estimated 1,000-5,000. The combined forces of America and South Vietnam declared the city as theirs again when the ARVN 1st battalion raised the Southern Vietnamese flag over the Palace of Perfect Peace in late February.
At Saigon, which is widely believed to be the main focus of the attack, with Hue and Khe Sanh being diversionary battles, with the Viet Cong and PAVN not seeking to take over the city, but rather strike at six focal points, including important ARVN airbases that housed their head officers, the US embassy, and most importantly, the National radio station. Highly optimistic statistics have the loss of like to the PAVN at 45,000, but the number is more realistically at around 30,000. The ARVN and US combine troops lost an estimated 6,000.
The objective of "war of attrition" had been achieved, but it was a crazy heavy cost to the morale of the United States troops. The general public had been promised that the war would be over soon, and that the enemy was running scared. The fact that the PAVN and Viet Cong could mount such a large-scale attack and do it in relative secrecy was unnerving to the American public who struggled to see why the mighty American war machine was struggling against this little group of communist insurgents.
What was the kicker for the tide turning in American public perception of the war, however, was the My Lai massacre. It occured in March of 1968, after everything except Khe Sanh was over from the first huge push of the Tet Offensive. Essentially American soldiers went into the village of My Lai and brutally slaughtered everyone there. Women were gang-raped and then murdered. Villagers were made to lie in a hole while grenades were tossed in. The massacre went public in November 1969 when Paul Meadlo admitted to the media that he had murdered anywhere from 10-15 civilians in My Lai. The public cried for an investigation, and they got it, but the results were far from satisfying. Every single soldier was acquitted* with the exception of Lt. Willam Calley, who only served three years of his life sentence before being released. The three servicemen (whose names I am unsure of) who attempted to stop the massacre were shunned by the military, and even denounced as traitors by several U.S. congressmen. They were awarded medals posthumously 30 years later.
The backlash from the uncertainty surrounding the actual strength of the Viet Cong, the perception of the military not really knowing how strong the enemy was, and then the subsequent cover-up of a crime against humanity that became internationally publicized shifted public perception of the war enough to affect the outcome, i.e. colossal public pressure to end the war and bring American soldiers home.
Everything is sourced from www.virtual.vietnam.ttu.edu, the national Vietnam archive online at Texas Tech university.
specific sources: The War in the Northern Provinces, 1966-1968
Vietnam: The Peer's Inquiry of the Massacre at My Lai