Waaaay back, the Republicans were the party of anti-slavery. It's why some of them cling to the idea of Lincoln so much. The Republican base was then in the north and the Democrat base in the South. Obviously, this is a pre-welfare state and both parties were run and voted for by property-owning white men.
Despite the franchise expansion, the ideology of each party didn't change away from classical liberal economics until the 1930s. Then, the FDR democrats adopted left-Keynesianism with the New Deal. This was possible because of the intense poverty during the great depression and the rising power of labour unions.
Later, during the 60s, Johnson signed the Civil Rights Act. This was then a divisive thing within the Democrats because white southerners were mostly opposed to it. Shortly after, the Republicans came up with the Goldwater Southern strategy and talked about states' rights. This was a racist dog whistle because everyone knew that giving southern states more power would be used to oppress black people. The demographics shifted in the '60s and '70s so that Democrats became the party of the north and the cities, republicans the south and rural areas. It's been that way since.
Then there was Reaganism which was what Thatcherism was in the UK. Then there was Trumpism and they became even worse!
Now up to ten hearts. I always find these quote appropriate whenever I see “Republicans freed the slaves that Democrats owned, and they’re still the party of the KKK!” :