The LDS Church essays that are linked to in the other answer are ostensibly correct: Joseph Smith granted black men the priesthood and it was Brigham Young who implemented the priesthood and temple restriction. However, the question about Mormons being abolitionists and then adopting pro-slavery views is much more complicated. The implication that the church made a conscious political decision to adopt pro-slavery and segregationist views is not correct. It had much more to do with the personalities involved and the individual church leaders.
First, we have to ask what is meant by "Mormons?" If it's the institutional church, we can look at early attitudes from church leaders like Joseph Smith, but there was never an official policy under Smith vis a vis abolition. He did seem to be more progressive than a lot of white Americans at the time. But there are gradients between abolitionism and support for chattel slavery, and most Mormon leaders fell somewhere in the middle. Joseph Smith expressed different views at a different times and, like a lot of white northerners, moved from opposition to abolition to tentative support for it, though his statements are few and far between. When he ran for president in 1844, he did seem to support a plan of abolition, but again, one or two statements cannot be confused with a strong church-wide policy. But if by "Mormons" we mean the general church membership, there were a lot of different attitudes. White southerners joined the LDS Church in the 1830s and some owned slaves. By the 1840s and 1850s, many Mormons were converts from Europe and were less inclined to see slavery as anything but barbaric.
One of the first conflicts with Mormons in Missouri came because a Mormon leader published an editorial inviting free blacks to move to Missouri and join the LDS Church. Missourians did not like that, to say the least. The conflict spurred some backtracking and a disavowal from Joseph Smith that Mormons were abolitionist. But Joseph Smith did allow black men to be ordained to the priesthood and took a black woman into his home to let her live with his family.
It was Brigham Young who solidified Mormonism's anti-black teachings. When Mormons were called to gather in Salt Lake City, the vanguard company included three slaves. Southerners who moved to Utah and other Mormon colonies were permitted to keep their slaves. Young gave speeches making it clear that he saw blacks as cursed descendants of Cain and Ham and he restricted access to LDS temples and to the priesthood for black members. Young was opposed by Orson Pratt, who gave surprisingly progressive speeches to the Utah Territorial Legislature denouncing slavery and openly disagreeing with Young. Pratt and Young often clashed over doctrine, theology, and in this case, church policies on slaves and people with black skin. Historians have teased out Young's views and, while he seems not to have been odious to slaves, he certainly did not object to it and he saw blacks as cursed, even filthy.
One example of how Young's rhetoric impacted Mormons: in 1866 a former slave named Thomas Coleman was murdered in Salt Lake City and a sign was put on his body that said "Notice to all niggers, leave white women alone!" While fears of miscegenation were hardly unique to Utah, and this murder was just one of thousands during the Reconstruction Era, Mormonism's teachings only reinforced the fears. No one was charged with the crime and no real investigation was carried out.
The race policy begun by Young hardened into church doctrine. Over time Young's teachings were echoed and adopted by other church leaders and church members; Joseph Smith's initial allowances granted to black members were forgotten. What started as a common nineteenth-century Protestant belief propagated by Young (that black skin is a sign of the curse of Cain and Ham) became an LDS doctrine that members believed was handed down from God. There are numerous statements from nineteenth- and twentieth-century leaders that the priesthood and temple restriction came from God and could only be removed by God. I point this out because, as attitudes changed, the LDS Church became the target of protests and rancor. They reversed the teaching in 1978, fifteen years after the March on Washington and many of the high points of the Civil Rights movement. The priesthood and temple ban became a source of embarrassment for the church, and members and leaders often try and paint the ban as a "policy." But make no mistake, leaders and members alike for well over a century were very clear that this was a church doctrine that only God could change. And even in 1978, church leaders had to wait until one especially vocal leader who opposed the change was out of town (Mark E. Petersen) before they could "unanimously" agree to remove the ban.
Young's teachings and enforcement of the priesthood ban moved the LDS Church to mirror the broader American culture. Throughout the twentieth century church leaders and members in Utah supported segregation and enforced it in their own ways. When black performers like Marion Anderson came to Salt Lake City, they were allowed to stay at the church-owned Hotel Utah but were told they could not use the main dining room and they were required to take the service elevator. Church leaders spoke and wrote of the inferiority of blacks, of their curse, and of how it would likely always be this way. Joseph Fielding Smith and his son-in-law Bruce R. McConkie were especially vocal, but they were not the only ones. Mormonism's unique theology adapted to the ban and some church leaders taught that blacks were not righteous enough followers of God in the pre-existence. Mormons, in other words, were like most other Americans: they were not any more progressive, any more anti-segregation (quite the opposite), and they had little interest in reversing the priesthood and temple ban until the Civil Rights movement brought the question to the forefront.
For some excellent sources, see W. Paul Reeve, Religion of a Different Color: Race and the Mormon Struggle for Whiteness (New York: Oxford University Press, 2017); Max Perry Mueller, Race and the Making of the Mormon People (Chapel Hill: University of North Carolina Press, 2017); Matthew L. Harris and Newell G. Bringhurst, The Mormon Church and Blacks: A Documentary History (Urbana: University of Illinois Press, 2015); Armand L. Mauss, All Abraham's Children: Changing Mormon Conceptions of Race and Lineage (Urbana: University of Illinois Press, 2003); Quincy D. Newell, Your Sister in the Gospel: The Life of Jane Manning James, a Nineteenth-Century Black Mormon (New York: Oxford University Press, 2019).