It all started with an insult. During Andrew Jackson's 1828 presidential campaign, his political opponents labeled him a "jackass." Stubborn as he was, Jackson co-opted the insult and began putting a donkey on his election posters. For the rest of his career and even into his retirement, newspapers and cartoonists continued to represent Jackson either as a stubborn ass or struggling to control one.
Almost 40 years later, the donkey was used to represent not just Jackson, but a larger group of Democrats. In 1870, Thomas Nast, the German-born political cartoonist who gave us the versions of Santa Claus and Uncle Sam we know today, drew a cartoon for Harper's Weekly titled "A Live Jackass Kicking a Dead Lion." The donkey was a stand-in for "Copperhead Democrats" (the Northern Democrats that opposed the Civil War), and the lion represented Edwin M. Stanton, Abraham Lincoln's recently deceased Secretary of War. Nast thought of the Copperheads as anti-Union and believed the Democratic press's treatment of Stanton was disrespectful.
In 1874, the New York Herald loudly opposed the possibility of Ulysses S. Grant running for a third presidential term and cried Caesarism. Nast, a life-long Republican who'd become frustrated with his party, thought Republicans might fall for the scare tactic. He drew another cartoon for Harper's, again using a donkey to represent Democrats and adding an animal to symbolize Republicans.
The cartoon, titled "The Third Term Panic," showed a donkey (representing the Herald and the Democratic press) wearing a lion's skin (labeled "Caesarism") in order to frighten a group of animals. Among those animals are an elephant (labeled "Republican Vote" and awkwardly fleeing towards a pit labeled "Inflation" and "Chaos") and a fox (labeled "Democrats" and backing away from the pit that the elephant is about to fall into).
The Republicans lost control of the House of Representatives that November, and Nast bemoaned the defeat in another cartoon. It showed an elephant caught in a trap set by a donkey, and the lumbering confused behemoth of the Republican Party undone by the Herald's scare tactics.
Nast continued to use the elephant and the donkey in his cartoons, eventually having them represent the whole of his party and the opposition. In March of 1877, after Republican Rutherford B. Hayes' controversial victory, a Nast cartoon showed an injured elephant ("Republican Party") kneeling at a tombstone labeled "Democratic Party." An 1879 cartoon (pictured) showed a politician grabbing a donkey labeled "Democratic Party" by the tail to keep it from falling into a pit of "financial chaos." The Republican elephant ("the sluggish animal") is lying on and blocking the road to an election victory.
By 1880, other cartoonists had picked up the symbols and spread them across the country. Over a century later, their continued use in cartoons, party literature, campaign buttons and all sorts of political merchandise and propaganda has cemented the association between the parties and their animals. The Republicans have even adopted the elephant as their official symbol (the Democrats have yet to do the same for the poor donkey).