The French occupation of Mexico ended because the United States' assistance to the Mexican army was turning the endeavor into a costly one for France. Napoleon III felt that France's military interests were better served elsewhere.
France originally invaded Mexico because Mexico was an easy target. The country was bankrupt and France had a very strong army. Mexico owed France and several other European nations a significant amount of money, which it was unable to pay back because of an economically devastating civil war during which it tried to break ties with the Catholic church.
Although France's initial invasion was unsuccessful, France went on to take control of Mexico. In total, France only occupied Mexico for about four years. France turned its interests to the push of Prussia onto home soil and abandoned the cause in Mexico, leaving Ferdinand Von Habsburg, the leader who had been appointed by Napoleon III, to be executed. Cinco de Mayo, a holiday that is celebrated on May 5th each year in the United States and Mexico, marks Mexico's initial defeat of the French army at the Battle of Puebla in 1862, which was before the French went on to overtake Mexico's military and establish the occupation of Mexico.