Weren't working conditions horrible for hundreds of years before then?
There were a number of reasons that unions did not form before that (even though they did actually start to form a few years before 1840).
When the industrial entrepreneurial bourgeoisie started the industrial revolution, they also created the working class that would supply the labor for this industry. This new workers-employers cleavage was new in it's geographical concentration around urban centres, as the most predominant cleavage before that had been the rural-urban cleavage (even though they existed and still exist somewhat independently).
Industrialization had a very deep impact on Western societies. It radically changed the production mode, caused unprecedented geographical mobility through urbanization and also transformed family structures. Living conditions in these new industrial centres were extremely poor.
The high concentration of workers in the cities and the poor working conditions made it easy for the workers to mobilize through unions. Socialism often provided a unifying ideology. They campaigned especially for labour protection (against the capitalist economy), social rights and welfare and political and legal rights. Many of modern Europe's socialist and labour political parties originated as Unions when they achieved universal suffrage for their members.
So weren't working conditions horrible for hundreds of years before then?
Yes, yes they were in many cases. However, the mere scale on which workers became concentrated in the cities during the industrial revolution created a powder keg for the emergence of unions or union-like structures. It should also be noted that this was really only possible because many governments did not want violent conflicts with the workers or risk civil war in some cases. Beforehand, peasant revolts (rural-urban cleavage) and slave revolts were the most common type on workers' protests and were often put down violently. So basically, change in attitude towards workers, change in mobility from rural to urban and high concentration of workers in the cities created the perfect atmosphere for unions to form.
What about the US? I realise this is not part of your question, but when examining why unions started to form some places (particularily in Europe), i also find it reasonable to examine why they didn't form in other places, especially in a country as influential as the US. Caramani proposes the following central explanations:
The historical abscence of influential workers' unions (and of actual labour parties) is also a major part of the explanation that socialist parties have never gotten a real foothold in the US.
I hope this answered your question, feel free to ask follow up questions.
Source: Daniele Caramani; Comparative Politics, ch. 13