It's a complicated story, but here's the short version:
The Republican Party was formed from the abolitionist remnants of the defunct Whig Party in the 1850s. The Democratic Party split during the 1860 presidential election, with the pro-slavery southern Democrats unable to agree on a candidate with the northern Democrats. Lincoln, the Republican nominee, won because his opposition was split. Fearing that Lincoln's election was the beginning of the end of slavery, the southern states seceded, the Civil War was fought, and the Confederacy was defeated in 1865.
The important thing here is that the Republican Party was the party of Lincoln, emancipation, and preserving the Union. For several years after the war, Union soldiers occupied much of the South and the Republicans dominated American politics. What remained of the old Southern elite resented the end of slavery, the devastated economy, and their loss of power.
They gradually clawed their way back and regained control of most of the state governments in the old South in the 1870s. And they were almost all Democrats because they couldn't vote for the party that destroyed their political systems and deprived them of their human property.
These Southern Democrats (often called "Dixiecrats") became awkward partners with northern Democrats. They were far more conservative than the northerners, refused to be Republicans, and knew that being a third party would make them irrelevant. The most practical path for both Democratic factions was to team up.
This uneasy alliance continued well into the 1950s, when the burgeoning civil rights movement began causing more public schisms. President Lyndon Johnson took up the cause in the 1960s, spearheading the Civil Rights Act of 1964 and the Voting Rights Act of 1965 with mostly Republican votes. Johnson won the 1964 election in one of the biggest landslides ever. LBJ's opponent, Barry Goldwater, had courted the South, and although he lost badly, he planted the seeds for the Republican courting of those Dixiecrats.
In the run up to the 1968 presidential election, the Republican nominee, Richard Nixon, took that courting to the next level, using racial code words to appeal to the Dixiecrats' opposition to civil rights, while also appealing to other Americans' desire for order. This "Southern Strategy" helped Nixon narrowly win the election.
This accelerated the shift of conservative southerners toward the Republican Party, although legislators were much slower to switch parties. Over the next three decades, Republicans steadily gained in the South, so much so that the only Democratic presidents during that span (Jimmy Carter and Bill Clinton) were both southerners. That shift was almost entirely complete in the 1990s.
Today, Republicans dominate the Old South, although as in most of the country, the split is mostly urban vs. rural now. (Big cities in the South like Houston and Atlanta tend to vote Democratic.) It took a number of political shifts over many decades to cause southerners to vote Republican instead. There were a few "enemy of my enemy is my friend" moments along the way, and down-and-out Republicans needed a partner in the 1960s. It took about a hundred years for every former Confederate and their children to die out, so the Civil Rights movement came along just as southerners with no direct memories of the Civil War were eager for a new alliance.
In countries with multi-party political systems, parties often splinter into new parties. But in a two-party system like the United States has, major shifts happen when some members of one party defect to the other. There have been other shifts like this in US politics, but the defection of southern conservatives to the Republican Party has been the biggest recent one.