I'm not a Church leader, so I don't know if what I have to say is exactly what Church leaders would have told you had you brought these concerns to them, but I did take a history class on basically these issues (at least some of them). My professor was a bishop, and one of the leading experts on Church history in the world. Apparently these days he's in charge of the Maxwell Institute, a research institute owned by the Church. However, he was also very real about the seriousness of many of the topics, and didn't try to sugarcoat anything, so I really appreciated his take on things. Hopefully something I say can be of help, although if you would like to talk to someone with more expertise than I, shoot me an email at alta(at)theboard.byu.edu and I can get you in contact with my professor, Dr. Spencer Fluhman. In writing this answer I tried to link to as many official Church sources as possible, to give you the most official response I can, although I am admittedly not a spokesperson for the Church, and everything I say here is a conglomeration of official Church stuff, historical context, and my own opinions and conjecture.
Why Joseph Smith ran for president
To sum things up, basically it's because members of the early Church were deeply disillusioned with the United States leadership after seeing them fail the Mormons many times.
In the 1830s a large group of LDS pioneers moved to Missouri after Joseph Smith had a revelation saying Independence, Missouri, was to be the site of the new Zion. At the time Jackson County, where Independence was located, was mostly filled with rough and tumble settlers, who were disturbed by a huge group of what they saw as religious fanatics moving in. From the Saints' point of view, their own behavior made sense. They wanted to stay together because they had found a group where they fit in, and they were all able to mutually support and help each other. But the other Missourians didn't understand them and their odd behavior, and for as much as the Mormons said they weren't politically involved, they all had the same views and voted the same way, which seemed like a huge potential threat to the people who already lived there. The tension between the Saints and everyone else boiled over in a series of violent attacks on the Mormons by mobs, which you can read about here. This all culminated in Governor Lilburn W. Boggs' infamous Extermination Order in 1838, mandating that Mormons either be driven from the state of Missouri or exterminated (explicitly violating the 1st Amendment which guarantees freedom of religion). As a result virtually all the Latter Day Saints in Missouri were forced out of their homes during the winter of 1838-1839 with nowhere to go to, and Joseph Smith was unlawfully imprisoned in Liberty Jail in horrible living conditions. The Saints relocated in Illinois, where many of them died due to disease. Following the Extermination Order a mob also attacked a group of Saints living at Haun's Mill, brutally killing 17 people, including a child who was only 9 years old, and injuring many more.
In late 1839, after being released from Liberty Jail, Joseph Smith went to President Martin Van Buren to seek redress for the many illegal persecutions the Saints had suffered in Missouri. Although Van Buren initially seemed sympathetic to their cause, when Joseph Smith met with him again in early 1840, Van Buren told him, "Your cause is just, but I can do nothing for you. … If I take up for you I shall lose the vote of Missouri." This was understandably incredibly frustrating, because the Saints had just suffered years of illegal mob action, and even though the president of the United States agreed that they deserved justice, he refused to help them because it would be an unpopular move.
So when the election of 1844 rolled around, the Latter Day Saints were looking for a candidate who wouldn't simply ignore the problems they were facing. In late 1843 Joseph Smith wrote letters to the five main presidential candidates, detailing the abuse in Missouri and asking what they would do about it as president. Only three of the candidates even responded, and they did so with very little sympathy to the persecution the Mormons faced. So, in January of 1844, Joseph Smith announced that he was running for president as an independent. He had a detailed party platform which called for things like the abolition of slavery and the expansion of United States territory if they obtained the permission of the Native Americans already living on the land. He didn't specifically address the illegal mob action against Mormons in Missouri, but he did say that the chief magistrate should have "full power to send an army to suppress mobs … [without requiring] the governor of a state to make the demand." This would prevent anything like what had happened in Missouri ever happening again.
Joseph Smith himself said,
I would not have suffered my name to have been used by my friends on anywise as President of the United States, or candidate for that office, if I and my friends could have had the privilege of enjoying our religious and civil rights as American citizens, even those rights which the Constitution guarantees unto all her citizens alike. But this as a people we have been denied from the beginning. Persecution has rolled upon our heads from time to time, from portions of the United States, like peals of thunder, because of our religion; and no portion of the Government as yet has stepped forward for our relief. And in view of these things, I feel it to be my right and privilege to obtain what influence and power I can, lawfully, in the United States, for the protection of injured innocence (Joseph Smith, History of the Church, 6:210–11).
He was assassinated in June of 1844, during his candidacy for president, but before the election occurred.
It's also important to remember that church and state weren't nearly as separated as they are now at this time in US history. That made it a lot less weird that the prophet of the LDS Church would run for president. We also have to remember that a lot of Church members believed the Second Coming was imminent, and they were honestly concerned about the state of the nation if Christ were to come at that time, because their personal experience was that the United States was full of mobsters and corrupt politicians. They saw Joseph Smith running for president as a way to not only save the Church, but help save the country, as well. Since then the Church has said that its foremost leaders can't run for public office, and is now strictly non-partisan and politically neutral.
For more detail about Joseph Smith running for president, and as a source for a lot of the information here, see this article.
Brigham Young being bigoted
So first of all, Brigham Young appeared to be bigoted against other races because he was bigoted against other races. He was a product of his times, and unfortunately, his times were ones where white people were widely considered superior to everybody else simply because they were white. Nobody exists in a vacuum, and it's virtually impossible for someone to completely throw off the preconceptions and ideas that are ingrained in them by their culture, society, and personal experiences.
I know that it's hard to accept that a true prophet of God was so visibly imperfect, because shouldn't the prophet be better than everyone else? The thing is, though, literally everyone on earth is far from perfect. It is impossible for God to pick a perfect prophet, because nobody will ever fit that bill. God could maybe force the prophet to be perfect, but that would go against our agency to make decisions for ourselves, and honestly I would be more uncomfortable with a God who forced us to be perfect by taking away our choices than with a God who allows for some trial and error. Despite his imperfections, Brigham Young had the traits the Church needed at the time in its leader to help it continue to exist, and later prophets have had the traits needed to correct some of the problems that Brigham Young brought about.
Another factor in Brigham Young's more racist proclamations is the fact that he was actively trying to make the Church seem "more American." At the time, the LDS Church was seen as not white and not American, and that led to a lot of problems for them. In response, the Church (Brigham Young included) worked very hard to become more acceptably white, something that ended up leading to some very racist policies and speeches.
Like I already mentioned, the vast majority of Americans at the time were very racist, and they were suspicious of anything that seemed to threaten their idea of what an American should look and be like. As such, they were incredibly suspicious of the LDS Church. By the time Brigham Young was prophet, it was well known that the Church practiced polygamy, and that horrified most of the general public. There were concerns about child brides and morality, but perhaps more surprisingly, there were also concerns about polygamy being practiced in the United States because it was seen as an "Oriental" practice. People were really horrified about such an un-American practice being practiced in America, and that led them to think of the LDS Church and its members as un-American and even not white, despite the fact that most of them had come from places like England and Scandinavia. At the time, being "not white" was about the worst thing that could happen to an organization in the United States, and the perception of Mormons as their own separate "Mormon race" helped propagate the idea that Mormons deserved all the persecution that came their way, and did not deserve religious freedom. This article is very well-researched and does an awesome job explaining how the Church was painted as not nearly white enough, both because of polygamy and because of their tolerant stance on other races, and how that led to a lot of problems for the Saints.
Oh, also, in case it's not clear enough already that the Church faced a lot of problems for not being "white enough," the original Republican Party platform was dedicated to eradicating the so called "twin relics of barbarism," polygamy and slavery. That's right, an entire political party was formed to not just get rid of slavery, but also to force the LDS Church to stop being so barbaric with its un-American practices of polygamy (and as another part of that, intermarrying with other races, something that was seen as almost as heinous as polygamy itself). One of the huge problems that the American public, and also the American government, had with polygamy was that it was seen as an "oriental" practice, and not something that should be practiced in white America. Because the Church wasn't about to get rid of polygamy any time soon (I'll get around to that later), they had to resort to other measures to try to make themselves seem more white and American to the public to try to decrease some of the persecution and mistreatment they regularly faced. So, that meant they started disparaging other races, becoming more racist to fit into a racist society a little better. Or, as Paul Reeves, author of Religion of a Different Color: Race and the Mormon Struggle for Whiteness, said, "The irony is that they start participating in the same racial construct that was denigrating them." So part of the reason Brigham Young made so many comments that we now understand as racist is that he really did believe that, but part of the reason also could have definitely been that he was operating in a society that hated the Church because of its supposed un-Americanness.
Why African Americans couldn't hold the priesthood for so long
In the Church's inception, Joseph Smith actually had no problem with blacks being ordained to the priesthood, and even personally ordained at least one black man, Elijah Able, to the Melchizedek Priesthood. Elijah Able also was able to attend the temple and did baptisms for the dead there. And as we saw in Smith's bid for presidency, he was against slavery. In 1847 Brigham Young called a black man, Q. Walker Lewis, "one of the best Elders" of the Church in a private meeting. So in response to your point about God's church being pure from the start, it was. It was only after more than twenty years of acceptance toward African Americans that it changed its policy to exclude blacks.
In 1852 Brigham Young made the official statement that men of "black African descent" could not be ordained to the priesthood, though they could still be baptized and ordained members of the Church. (Subsequent prophets extended this ban to the temple, as well.) However, at the same time as Young revoked the right of black men to be ordained to the priesthood, he also said that they would eventually "have [all] the privilege and more” given to other members. Clearly the priesthood ban was never meant to be a permanent thing.
So, why did the Church change its stance? Why did it start excluding blacks? Well, for one thing, remember what I said about how racist America was at the time, and how the Church faced a lot of problems for not being racist enough to fit in? I think that definitely applies here. At the time, blacks were widely regarded in America as property, not people deserving of rights and respect. While there were a few outspoken abolitionists, the vast majority of Northerners were pretty apathetic with regards to slavery, as long as it wasn't happening in their state, and most of those who did oppose slavery still didn't believe that blacks should receive the same rights as whites. Meanwhile in the South, many prominent leaders were making the argument that slavery was a positive good that was actively benefiting all parties involved. It was in this climate that Charles Sumner, a Northern abolitionist senator, was caned almost to death on the House floor during a session of Congress by a representative from South Carolina, simply because Sumner had given a speech opposing slavery. The representative who caned him was met with a parade in his home town. It was also in this climate that the Supreme Court case Dred Scott v Sandford codified racism into national law with its decision that blacks had "no rights which the white man was bound to respect." Although the caning of Charles Sumner and the Dred Scott decision happened in the years following Brigham Young's statement about blacks and the priesthood, they're pretty indicative of the rising tensions about African Americans at the time, and how deeply unpopular it was to advocate for, or even openly support, black rights. That was bad news for a Church that allowed for total integration of blacks with whites, and whose leaders advocated for black rights.
Now we've got the political climate set up, enter Brigham Young. In 1850, Utah was made an official territory of the United States. This meant that although they had none of the rights of statehood, they did have the obligation to follow the Constitution and were subject to jurisdiction from Congress. This was basically the worst of both worlds for the Mormons, who had purposely relocated in what was currently Mexico in order to escape the United States (remember all their issues with the government?) However, as a territory they had none of the freedom they had hoped for as a small colony in Mexico, yet none of the rights of states. This caused them to be pretty highly motivated to try to become a state, not because they particularly loved the US, but because they wanted more autonomy. As governor of the Utah territory, Brigham Young's decision to instate the priesthood ban could well have been influenced by some political motivations to get in with the federal government and help to Utah become a state. I don't know, because obviously I'm not Brigham Young and I didn't make that decision, but from the context at the time, that could make sense.
Once the ban was in place, people, including Church leaders, came up with all sorts of rationales trying to justify it, such as adopting the popular idea of the time that black people were inferior because of the "curse of Cain," or because they were supposedly less faithful in the war in heaven. These ideas were NOT doctrine; they were weird ideas supported with scanty evidence pulled from a few misinterpreted verses of scripture. However, they gained a lot of momentum, probably because people were eager to find a reason for the ban, so they took any idea they could get and ran with it. Unfortunately, a lot of these ideas were taught as doctrine at the time by Church leaders, and that's tragic. With the benefit of hindsight we can look back and say that they definitely weren't doctrine, but in the thick of it there were a lot of people who said they were. What does that mean about those Church leaders? Well, for one thing it means they were people. People are fallible, and they try to come up with reasons and justifications for bad things, and sometimes those justifications aren't great. Church leaders were fallible, too, and they demonstrated that with some of the racist rhetoric we see from this time period. However, if it gives you any hope to know this, at the same time there were Church leaders who actively opposed the racist ideas supporting the priesthood ban and fought against them, so it wasn't a Church-wide thing, just individual fallacies. The fact that the First Presidency and Quorum of the Twelve never officially stated that the reason for the priesthood ban was because of the "curse of Cain" or "blacks were less faithful in the premortal life" also means that those ideas were never accepted by the Church as doctrine, and any attempt to pass them off as such was merely individual conjecture.
The problem is, once an idea catches hold it's hard to get rid of. The simple force of momentum can keep a lot of things going, and in my non-expert opinion, I think that's what happened with the priesthood ban. It was getting harder and harder to believe any of the rationales about why those of African descent couldn't hold the priesthood or go to the temple, especially in view of the sacrifices and faith of Church members of African descent across the globe, but with the weight of years of that practice behind them, it was hard to change course suddenly. Think of a yacht: you're not going to get a ship of that magnitude to turn 180 degrees all at once. An organization like the Church is like a giant yacht, where big changes don't just happen (for example, the missionary age change from a couple years ago only took place after months of meetings and pondering, and the missionary age is something that hasn't been as deeply defended as was the priesthood ban).
Why would God allow His Church to instate a racist policy? Well, first of all let me just remind us all of what I said earlier, about how we all have agency, and even if we do dumb things with that, God isn't going to stop us. Going along with that, He's not going to force answers down someone's throat, even if that someone is a prophet. Until a prophet could escape the weight of history and the racist context/rhetoric of his own day, the ban was going to hold. Second of all, I have no idea how things with Utah territory and the Church would have panned out had the Church continued to promote full integration of blacks. It's possible that that plus polygamy would have been too much for the US government, or even just common Americans, to handle, and things with the Church could have turned out much differently than they did. This is murkier water here, because it's impossible to quantify, and I don't want to say that the individual suffering of black members of the Church can be quantified and justified in relation to the overall well-being of the Church, because we have absolutely no idea what would have happened without the priesthood ban, but this may have been a contributing factor.
For a lot of my information in this section, see here. But just to be clear, this is the section where I had to make the most inferences, and everything I said about why the Church might have instated the priesthood ban is my own personal thought, backed up by official Church sources as well as historical research. So I didn't just pull this stuff out of nowhere, and I feel that what I said is pretty well backed up, but I do want it to be clear that the Church hasn't given an official reason for the ban.
Why the Church continued to cling to polygamy for so long
First of all, from your question it seems like you have a mistaken idea of why the Church practiced polygamy in the first place. I may be reading this wrong, but your question makes it seem like you think they practiced polygamy because there was a shortage of men due to the Mexican American War, so in order to temporally take care of all the women in the Church they decided to start practicing polygamy so every woman would have a provider. That's a popular defense of polygamy that I've heard from all sorts of people, including missionaries at Temple Square, because it's an easy explanation that at least somewhat makes sense to us and is harder to attack from a moral point of view. The thing is, though, it's wrong. The Church had been practicing polygamy for a long time before the Mexican American War, and as you pointed out, they continued to practice it for a long time afterward. There might be some sort of argument in there about helping provide for women because there were fewer LDS men than women at the time (I don't know if that's true or not), but the revelation instructing Joseph Smith to practice polygamy says nothing about that. The reason the Saints practiced polygamy is that God told them to do it. It was hard, and I honestly don't know why He told them to, but from their point of view, despite how difficult it was, they were doing it for the logical reason of obeying God's commandments.
One of the most commonly accepted reasons for polygamy in the early Church (and the one we have the most evidence for) was to "raise up seed unto [the Lord]." Jacob 2:30 in the Book of Mormon uses that phrase, and it's when Jacob is actually telling off the men of the Church at that time for taking more than one wife. He says that they were justifying it because Solomon and David did it in the Bible, but he also says that God has said that polygamy absolutely shall not be practiced just because someone feels like it, or thinks it's the right thing to do. Jacob quotes the Lord and says,
Wherefore, my brethren, hear me, and hearken to the word of the Lord: For there shall not any man among you have save it be one wife; and concubines he shall have none; For I, the Lord God, delight in the chastity of women. And whoredoms are an abomination before me; thus saith the Lord of Hosts. Wherefore, this people shall keep my commandments, saith the Lord of Hosts, or cursed be the land for their sakes. For if I will...raise up seed unto me, I will command my people; otherwise they shall hearken unto these things. (Jacob 2:27-30, emphases mine).
So, here we have God saying that polygamy is unacceptable, unless He specifically commands otherwise in order to raise up seed unto Him, or in more modern terms, in order to have more children born in the bounds of the Church. God does reserve the right to command polygamy, but for the reason of having more children be born in the covenant. In the early days of the Church there were incredibly few members, and although growth was relatively fast, there still weren't a ton of Mormons in the world. Polygamy did lead to the birth of a lot of children in the Church, and greatly helped its membership. Those children went on to do amazing things and help the Church and the world in a lot of ways, and if you look at a lot of members of the Church today, they come from a polygamous heritage. I personally only exist because of polygamy, so my selfish reason for being glad it happened is being grateful that I'm alive.
Another possible reason I've heard for why the early Saints practiced polygamy is because the Restoration of the Church was a restoration of ALL things (as Elder James E. Faust called it). Well, if we're going to restore everything that ever happened in any of the previous dispensations, one of the things we restored had to be polygamy, because we do have evidence of it being practiced anciently in the Church. According to those verses in Jacob, they only practiced it anciently in order to build the righteous membership of the Church, which is also why it was practiced modernly, but the thing is, if we're restoring everything, polygamy is on that list. So it had to be practiced at least briefly in this dispensation, but was later discontinued. This isn't doctrine, no Church leaders have come out and said that we practiced polygamy in the 1800s because it was practiced anciently and we had to restore everything, but it's an idea that sits well with me. You can take it or leave it, it's just an idea I heard that personally made sense.
Okay, so now that we sort of know why the early Saints practiced polygamy, lets look at why they defended it so vigorously. There are firsthand accounts of both men and women detailing just how difficult and heart-wrenching it was for them, so why would they continue to live it? Why would they defend it, and even call it a good thing? For one thing, at the start they had an incomplete understanding of sealings, because not a lot of information about temple sealings and eternal families had been revealed yet. They knew that it was necessary to be sealed to get to the highest level of the Celestial Kingdom, and that in heaven everyone was going to be one big family, meaning that everyone was going to somehow be sealed to each other. However, the practice of vicarious sealings for the dead hadn't happened yet, so if you had parents or grandparents who died without the gospel, they thought there was no hope of being sealed to them. Without being able to create a ladder of sealings going up through the generations, they thought they had to do a web of sealings, going out horizontally to everyone who lived concurrently with them rather than going up vertically to past generations. One way to do that was polygamy. So one reason they approved of polygamy was because they saw it a a way of creating an eternal family, and despite the hardship involved they thought that was worth it and necessary. (I don't have time to look up the source for this right now, but if you either email me or submit another question I can get find the source for you).
Going along with that, another reason the early Saints continued to uphold polygamy despite their own personal feelings about it is that they saw it as the most surefire way to gain eternal salvation, and their focus was the eternities, not the here and now. They thought it was necessary to deal with hardship in this life for celestial rewards in the world to come. Annie Clark Tanner was a polygamous wife in Utah, and in her autobiography A Mormon Mother she talks about how the Church, and the individuals within it, tended to emphasize the hardness of the gospel, and the strictness and condemnation of God. Polygamy, along with all its trials, fit very easily into that framework of the prevailing culture, something that might not have happened if they had emphasized the joy of the gospel, and the love and mercy of God like we do now. But because they sort of expected life to be hard and God to ask them near impossible things, they were more ready to accept polygamy. The Church also made it fairly easy for women to get divorces, to have an escape valve from the pressures of polygamous life
Finally, another thing that I've noticed in a lot of personal accounts of people who lived through polygamy is that they saw it as a way of unifying the Church and binding them together. We're called a "peculiar people," and the peculiar practice of polygamy certainly cemented that. The internal suffering and outside persecution they faced as a result really created bonds of unity in the Church, because common suffering is one way to bond with someone fast (just think of Harry, Ron, and Hermione becoming friends after being attacked by a troll). With that positive effect of polygamy in mind, along with the hope of eternal rewards and satisfaction, mixed with the idea that they were creating a huge eternal family through polygamous sealings, the Saints overall were fairly supportive of the practice, despite any personal hardship they had with it.
Due to the huge support polygamy had in the Church, and the immense faith of the early members who were willing to do anything God asked them to, polygamy had a lot of momentum and force behind it, meaning that they weren't going to give it up for almost anything. When they did stop practicing it, it had to be as a result of an official declaration from the prophet, because they were so faithful they wanted to be sure the command to stop was coming from God.
Why would God suddenly change His stance on polygamy? I don't think He did "suddenly change," He just went back to the norm. The Saints had pretty successfully raised a lot of children in the Church, so there was no longer any need to keep practicing polygamy. They had enough members that the Church could be self-sufficient and self-sustaining without having to rely on polygamy to replenish the ranks, and God has been pretty clear in the scriptural record throughout all of history that polygamy is not the norm, it's only a rare exception. The conditions necessitating that exception had come to an end, so they returned to the normal state of affairs (aka monogamy). Second of all, things were looking pretty dire for the Church's continued existence within the political climate. First there was a Supreme Court case that specifically banned the practice of polygamy for religious reasons, and then there was a succession of national laws that outlined specific punishments for polygamy, and that threatened the Church's ability to continue to function as a religious institution. Faced with the choice to either keep practicing polygamy and lose the right to even have a church, or discontinue the practice of polygamy, Wilford Woodruff turned to prayer to find out what he should do, and then issued the Official Declaration 1 of the Church, officially discontinuing polygamy. It might seem a little weird for a revelation to come about as a result of a political situation, because we think of the Church as separate from politics, but personally, I don't think it's wrong for revelation to be preceded by a sincere question, like this one was, or for that question to be motivated by the current world situation, like this one was. It simply shows that President Woodruff did not live in a bubble and was aware of the consequences of the Church's actions.
Look, I know that you're truly wondering and trying to find out the truth. And I know that there are some real, hard questions about the Church and about Church history, so I'm in no way blaming or condemning you for having these feelings. When I first found out from first-hand accounts about a lot of the terrible things that have happened in the Church, and the toll they took on real people, I was shocked. I realized how fallible people are, even leaders of the Church, and for a while that really shook my faith in the Church. It was hard for me, and I had to grapple with a lot of questions that we don't have answers to. However, eventually I realized that I couldn't deny what I did know and what I have felt based simply on the realization that there are still lots of things I don't know. My questions about a lot of these topics didn't change the fact that I know I've felt God's love for me. They didn't change the fact that I know I've prayed about the truthfulness of the Book of Mormon and had it confirmed to me. They didn't change the testimony I had of Christ's reality, and the saving power of His atonement. That doesn't mean that my questions and doubts weren't legitimate, or that yours aren't, either, just that all my other feelings were also legitimate. I still don't have all the answers, and there are still things that I don't understand. But I chose not to let those questions overshadow everything else. I decided to keep believing in the Church, despite my questions about certain things, because I feel good when I read the scriptures and pray and go to the temple. I believe that there are answers to these questions, just that we don't have them right now, and that's a part of life, so for now I'm just going to shelf those questions and wait for the day when I can talk face to face with God about all of them.
Obviously you're in charge of your own life, and you can make whatever decision you choose, but my suggestion is to not just look for the bad in the Church, but also remember the good. Why did you want to serve a mission? Why were you a faithful member? Don't ignore the bad things you've found out, but don't focus on them to the exclusion of the good things you've seen and felt. Try to find answers to your questions, and know that a lot of them really do have answers, but some of them don't have answers that we can explain right now. If you run into that problem, be patient and see if any more light or understanding comes with time. Remember that everything has a bias, and don't take all information you see at face value. Your final decision about what to do is in your hands, and your worth as a person will not diminish no matter what you choose. I'm sorry that this has been so hard for you, and I hope that something I've said here has helped answer at least a few of your questions.
P.S. I'm so incredibly sorry for keeping this question so long over-hours. Hopefully it's still relevant to you.