This is partially answered in the book itself, first of all in the initial description of the troll's lair:
There were bones on the floor and a nasty smell was in the air; but there was a good deal of food jumbled carelessly on shelves and on the ground, among an untidy litter of plunder, of all sorts from brass buttons to pots full of gold coins standing in a corner. There were lots of clothes, too, hanging on the walls-too small for trolls, I am afraid they belonged to victims - and among them were several swords of various makes, shapes, and sizes. Two caught their eyes particularly, because of their beautiful scabbards and jewelled hilts.
And secondly when Elrond examines the swords:
That day he looked at the swords they had brought from the trolls' lair, and he said: "These are not troll-make. They are old swords, very old swords of the High Elves of the West, my kin. They were made in Gondolin for the Goblin-wars. They must have come from a dragon's hoard or goblin plunder, for dragons and goblins destroyed that city many ages ago."
So while there is definitely a very large gap in the history of particularly the swords, it seems clear that much of the treasure was sourced from victims of the trolls, and supplemented by items somehow obtained from "a dragon's hoard or goblin plunder" (but Tolkien never states clearly how or when this happened).