The United States exists because of immigration and of immigrants. Literally everyone came to America from somewhere else, except the Native Americans (but if you want to get technical, they crossed over on the Bering land bridge, surprising the local animals).
The point is, from the moment English people stepped off of boats in the 17th century, there was always a new wave of immigrants.
The other important thing to remember is that one of the most unpopular groups in the U.S. is whoever makes up that new wave of immigrants. So in the 1840s and '50s, when the Irish were fleeing the Potato Famine, they were unpopular. Later on, it would be the Germans, and later on the Italians. It was even worse for non-white people, as they found it harder to assimilate into the prevailing white culture they found. (Mostly because when they said, "Hey, I'm new to the neighborhood," a racist would eventually say, "You look slightly different from me! Eeek!")
This negative attitude goes right back almost to the birth of the country. The United States of America was founded in 1776. By 1790, we were already restricting immigration. That tells you everything you need to know, right there.
The Gold Rush in California, and the later First Transcontinental Railroad, created a huge demand for labor. This meant more people coming in to work, which included people coming from as far away as China (although you probably guessed that because you read the title of this page). Chinese laborers increased the supply, which drove down the price of labor.
And no one likes making fewer wages.
This tension got worse when you threw in the vast differences in cultures. Americans weren't used to Chinese people…and they were ignorant. They didn't see a Chinese immigrant and think about the complexities of Chinese philosophy, the intricacies of Chinese art, the power of the Chinese military, or that some of the advances in engineering that China had made in the 1400s wouldn't be replicated in the Western world until the 20th century. (Source)
They didn't think: whoa, these Chinese-Americans are going to have serious influences on everything from architecture (Billie Tsien) to reporting (Ben Fong-Torres) to media (Steve Chen) to music (Yo Yo Ma) to politics (Steven Chu) to haute couture (Vera Wang).
Back in the 1800s, all the white Americans saw were people who looked strange, who were non-Christians for the most part, and who spoke a language they couldn't understand. Oh: and that they were making it harder to find work.
The Chinese were an easy group to paint as outsiders. Some groups latched onto the economics of the dispute, simply concentrating on the fact that so many laborers led to lowered wages. Other groups got more racist than that one uncle you don't want to invite to Thanksgiving. The end result was the Chinese Exclusion Act. This was later bolstered by several other laws, which systematically gutted the rights of Chinese residents.
Chinese residents did everything they could to fight the laws, and even made it to the Supreme Court, twice. In every case, however, the constitutionality of the laws was upheld. There wasn't really a whole lot they could do. The Chinese Exclusion Act, originally set to expire in 1892, got renewed twice, and the second time was given no expiration date at all.
World War II came to the rescue. China was a natural U.S. ally against the Japanese. By Pearl Harbor, Japan had already invaded China, and you know how the saying goes: "the enemy of my enemy is my friend." China was also a perfect place for American bombers to land after dropping their payloads. In 1941, the two countries were officially allies.
Two years later, the Magnuson Act finally overturned the Chinese Exclusion Act. That's right. 1943. Crazy, right?