Oh gosh, the answers and discussion here so far are quite bad.

First, we have to discard the idea that there was a specific year, or even decade, that the two parties "flipped". The history of politics is absurdly complex and involves lots of different players and there are rarely any discrete points in history you can point to and say, "this is precisely where this big ideological shift happened". It would be a lot like asking, "what year did tea party politics take over the Republican party?"

We also need to address the dog whistle in the air: modern Republicans love saying that they are the party of Lincoln and that it was Democrats that fought for slavery. They count on people having heard of this shift happening but not being well enough versed in it to understand where the lie is hidden.

Let's start with Andrew Jackson. He started as a member of the Democratic-Republican party but he pushed hard for an ideology that would become known as Jacksonian democracy [1]. This alienated a lot of fellow members of the Democratic-Republican party, and they joined forces with the remnants of the Federalists and a handful of others and formed the Whig party. The Democratic-Republican party became the Democratic party, led by Jackson. Neither the Whig party nor the Democratic party had views which exactly parallel any of today's popular political ideologies.

After the Mexican war, the loose coalition of Whigs started to fracture over the issue of slavery. A bunch of smaller groups, including former Whigs, joined together to form the Republican party, which opposed the expansion of slavery. Meanwhile, in the lead-up to the Civil War, the Democrats started to split into northern Democrats and southern Democrats ("Dixiecrats"). At this time, slavery is the leading political issue of the day, and don't let anyone tell you otherwise.

Lincoln runs as a Republican and wins. There's a Civil War, and Reconstruction begins. The northern Democrats had held a wishy-washy position on slavery, and as a result, they lost all political power. Industrialist political influences in the north became represented by the victorious Republican party, while the Democratic party held on to power in the south.

This is the part where people see a "pro-slavery Democratic south" and an "anti-slavery Republican north", but neither of these parties at the time are all that similar to their modern antithesis. The Republican party at this time has a conservative group and a progressive group, and they fight frequently over things like concentration of wealth (sound familiar?). Southern Democrats meanwhile focused most of their political efforts on fighting any and every expansion of civil rights for former slaves.

Conservative Republicans won the presidency with McKinley, but Teddy Roosevelt, a progressive, was his VP. McKinley gets assassinated, Roosevelt takes office, and proceeds to enact a bunch of progressive reforms. [2] This ultimately results in a lot of friction between Roosevelt and conservative Republicans and that leads to an internal fight, with Taft leading the conservatives and Roosevelt forming the Progressive party.

Along comes Woodrow Wilson. (Bear with me, we're getting there.) Wilson, a Democrat, has been frustrated by his party's inability to tackle any issues other than civil rights. He manages to gather enough political influence to take control of the Democratic party, getting it out of the hands of the Dixiecrats. [3]

The infighting in the Republican party meanwhile leaves southern blacks unprotected, so they get really unhappy with both parties. All of this comes to a head post-Depression when FDR comes along. He's a very egalitarian, populist, post-Wilsom Democrat, and he captures a lot of political power.

So the situation now is this: there is a broken Republican party in the north, with progressive Republicans and conservative Republicans, and there is a progressive Democratic party, and there are a bunch of unhappy Dixiecrats -- the "Southern Democrats". FDR manages to pick up a bunch of the progressive Republicans along with black voters, and then Truman comes along and continues FDR's racially progressive policies, so this is when black voters begin to vote as Democrats.

The Southern Democrats were nearly shut out of national politics altogether at this point, while northern conservative Republicans were kept on life support until Nixon. There's a Nixon-Goldwater fight over the Republican party, but Nixon is an absurdly shrewd politician, and over the course of several years, he's able to build an FDR-like coalition of multiple groups under the Republican banner, including the northern conservatives and the former Dixiecrats, who were thrilled to get some political power again.

Nixon though was still an old-guard Republican and he would barely recognize today's Republican party. But, from here, the remaining dots are easy to connect.

This was the shortest summary of 150 years of messy politics that I could come up with. I'm skipping over a lot of important details and there are probably some points here that can be argued by those better-versed in this stretch of history.

But the important part is that these two modern parties have not existed as constant political entities, and so they didn't "switch sides". They were just labels for loosely-associated smaller political and demographic groups, and those groups changed allegiances depending on the biggest political issues of the day.

[1]: https://en.wikipedia.org/wiki/Jacksonian_democracy

[2]: The excellent u/Samuel_Gompers with a lot more detail on this: https://www.reddit.com/r/AskHistorians/comments/1nolzz/why_w...

[3]: u/Samuel_Gompers again, https://www.reddit.com/r/AskHistorians/comments/yczua/can_so...