The Chinese were treated badly during the nineteenth century for two primary reasons: fear of economic competition and racism/xenophobia.
During the nineteenth century, the United States' economy experienced cycles of marked booms and busts. At the same time, the settling of the West and the need for labor to build industry and infrastructure projects made immigration a necessity. During economic bust cycles, when there were fewer jobs than available workers, this often led to animosity toward immigrants. Immigrant laborers, especially the Chinese, were disliked by more settled workers, as the settled workers feared that the Chinese would work for far less money in far worse conditions, making it more difficult to maintain a comfortable standard of living.
When this combined with racism, it created an environment in which the Chinese were not well-treated and were ultimately banned from immigrating to the United States through the Chinese Exclusion Act of 1882.