The LDS pioneers left Nauvoo, Illinois in February 1846
(source). At this time bigamy (which is the beginnings of polygamy) WAS illegal in the states that they lived in. The LDS pioneers left the United States in order to seek 'religious freedom'. They arrived in the Salt Lake Valley in 1847. In 1847, the territory that they arrived in (now known as the Utah Valley) belonged to the country of Mexico (source). In February 1848, less then a year (six months and nine days) after the Saints arrived in valley, the
Treaty of Guadalupe Hidalgo (Source 2) was signed. In this treaty the US acquired the area known as the Utah Territory, thus becoming part of the United States and all laws, rules and regulations of the United States were applied. Though, at the time, this was not heavily enforced.
In 1862 (sixteen years later), Congress issued the
Morrill Anti-Bigamy, which clarified that the practice of polygamy was illegal in EVERY US territory.
What is interesting about this little law is the bit of behind-the-scenes wrangling that Brigham Young and then President Abraham Lincoln did. As my source states,
The measure had no funds allocated for enforcement, and Lincoln choose not to enforce this law; instead Lincoln gave Brigham Young tacit permission to ignore the Morrill Act in exchange for not becoming involved with the Civil War.
Not that it makes it any better for the LDS Church to break the law (12th Article of Faith anyone).
And, there is more, the
Edmunds Act of 1882 that signed into law that polygamy was a felony in the US. You can also look at the Supreme Court Case
Reynolds v. United States, 1878 (source 2) that ruled that, "religious duty was not a suitable defense to a criminal indictment."
Tell me, when did the LDS Church stop practicing polygamy?