The first book where Tolkien published anything about Trolls was in The Hobbit, although Trolls are mentioned already in The Book of Lost Tales:
The Trolls that the readers meet in The Hobbit are based on the Trolls that was known from norse mythology and fairytales. They were described as very large persons (twelve to fourteen feet high), with great heavy faces, slow in the uptake, and mighty suspicious about anything new to them. They had legs that were as thick as young tree-trunks and wore stony boots, and they had to be underground before the sun rose, or else they were turned to stone.
When The Lord of the Rings was published, some new varieties of Trolls were introduced. The first mentioned was the Cave-troll that the Fellowship met in Moria. According to what is written in Appendix F, these Trolls were probably of the same kind that was described in The Hobbit, but they had been taught by Sauron what little they could learn, and because of that had their wits increased with wickedness. They spoke a debased from of the Common Speech.
At the end of the Third Age, the Olog-hai appeared in southern Mirkwood and in the mountain borders of Mordor. These were Trolls that were bred by Sauron; filled with the evil will of their master. They were a fell race, strong, agile, fierce, cunning and harder than stone. And to make matters worse, they could endure the Sun as long as Sauron’s will held sway over them. The only speech they knew was the Black Speech.
Trolls are clearly not an invention of Illúvatar, so where they came from is an open question, just as the origin of Orcs. Treebeard states that Trolls were made in mockery of the Ents, just as Orcs were made in mockery of Elves. But in Myths Transformed, we can read the following:
|“It seems clearly implied in The Lord of the Rings that trolls existed in their own right, but were 'tinkered' with by Melkor”|
|History of Middle-earth X, Morgoth's Ring - Myths Transformed|
So what does that mean? Were Trolls indeed a part of the Third Theme? Were they originally “good” creatures that Melkor saw fit to use to his own purposes?
Probably Tolkien were a little confused about Trolls as he were about Orcs, cause in Letters #153, the following can be read:
|“I am not sure about Trolls. I think they are mere 'counterfeits', and hence (though here I am of course only using elements of old barbarous mythmaking that had no 'aware' metaphysic) they return to mere stone images when not in the dark. But there are other sorts of Trolls beside these rather ridiculous, if brutal, Stone-trolls, for which other origins are suggested. Of course... when you make Trolls speak you are giving them a power, which in our world (probably) connotes the possession of a 'soul'.”|
Trolls never had any central place among the population in the works of Tolkien. We see them as a side-adventure to the Quest of Erebor, and also those same Trolls re-emerge in The Lord of the Rings when their stony remains frighten the hobbits.
Later in The Lord of the Rings we meet the Cave-trolls in Moria, and the Olog-hai emerge at the Pelennor Fields and before the Morannon. And through the Appenices, we also come to learn that Aragorn's grandfather Arador was slain by Trolls in the mountain north of Imladris in T.A. 2930.