Tolkien did indeed create the Ents for The Lord of the Rings, only inserting them into The Silmarillion as he revised it later. From a relatively early point (probably following the hobbits' encounter to Old Man Willow, who was an older character from Tolkien's poems about Tom Bombadil), he had decided to include a more elaborate tree-related scenario later in the story, although it still took some time for him to finalize the nature of the Ents.
He discussed his creative process in a letter written in 1955 (letter 163, to the poet W. H. Auden). The letter actually comes from before the printed edition of The Return of the King appeared, although well after it had been rendered in final form.
Take the Ents, for instance. I did not consciously invent them at all. The chapter called 'Treebeard', from Treebeard's first remark on p. 66, was written off more or less as it stands, with an effect on my self (except for labour pains) almost like reading some one else's work. And I like Ents now because they do not seem to have anything to do with me. I daresay something had been going on in the 'unconscious' for some time, and that accounts for my feeling throughout, especially when stuck, that I was not inventing but reporting (imperfectly) and had at times to wait till 'what
really happened' came through. But looking back analytically I should say that Ents are composed of philology, literature, and life. They owe their name to the eald enta geweorc of Anglo-Saxon, and their connexion with stone. Their pan in the story is due, I think, to my bitter disappointment and disgust from schooldays with the shabby use made in Shakespeare of the coming of 'Great Birnam wood to high Dunsinane hill': I longed to devise a setting in which the trees might really march to war. And into this has crept a mere piece of experience, the difference of the 'male' and
'female' attitude to wild things, the difference between unpossessive love and gardening.
...
It would have been a big task without anything else; but I have been a moderately conscientious administrator and teacher, and I changedp rofessorships in 1945 (scrapping all my old lectures). And of course during the War there was often no time for anything rational. I stuck for ages at the end of Book Three. Book Four was written as a serial and sent out to my son serving in Africa in 1944. The last two books were written between 1944 and 48. That of course does not mean that the main idea of the story was a war-product. That was arrived at in one of the earliest chapters still surviving (Book I, 2). It is really given, and present in germ, from the beginning, though I had no conscious notion of what the Necromancer stood for (except ever-recurrent evil) in The Hobbit, nor
of his connexion with the Ring. But if you wanted to go on from the end of The Hobbit I think the ring would be your inevitable choice as the link.
If then you wanted a large tale, the Ring would at once acquire a capital letter; and the Dark Lord would immediately appear. As he did, unasked, on
the hearth at Bag End as soon as I came to that point. So the essential Quest started at once. But I met a lot of things on the way that astonished me. Tom Bombadil I knew already; but I had never
been to Bree. Strider sitting in the corner at the inn was a shock, and I had no more idea who he was than had Frodo. The Mines of Moria had been a mere name; and of Lothlórien no word had reached my mortal ears till I came there. Far away I knew there were the Horse-lords on the confines of an
ancient Kingdom of Men, but Fangorn Forest was an unforeseen adventure. I had never heard of the House of Eorl nor of the Stewards of Gondor. Most disquieting of all, Saruman had never been revealed to me, and I was as mystified as Frodo at Gandalf's failure to appear on September 22. I knew nothing of the Palantíri, though the moment the Orthanc-stone was cast from the window, I recognized it, and knew the meaning of the 'rhyme of lore' that had been running in my mind: seven stars and seven stones and one white tree. These rhymes and names will crop up; but they do not
always explain themselves. I have yet to discover anything about the cats of Queen Berúthiel. But I did know more or less all about Gollum and his plan, and Sam, and I knew that the way was guarded by a Spider. And if that has anything to do with my being stung by a tarantula when a small child, people are welcome to the notion (supposing the improbable, that any one is interested). I can only say that I remember nothing about it, should not know it if I had not been told; and I do not dislike spiders particularly, and have no urge to kill them. I usually rescue those whom I find in the bath!
The next year (letter 180, unsent), he added:
So through being a philologist by nature and trade (yet one always primarily interested in the aesthetic rather than the functional aspects of language) I began with language, I found myself involved in inventing 'legends' of the same 'taste'. The early work was mostly done in camps and hospitals between 1915 and 1918 — when time allowed. But I think a lot of this kind of work goes on at other (to say lower, deeper, or higher introduces a false gradation) levels, when one is saying how-do-you-do, or even 'sleeping'. I have long ceased to invent (though even patronizing or sneering critics on the side praise my 'invention'): I wait till I seem to know what really happened. Or till it writes itself. Thus, though I knew for years that Frodo would run into a tree-adventure somewhere far down the Great River, I have no recollection of inventing Ents. I came at last to the point, and wrote the 'Treebeard' chapter without any recollection of any previous thought: just as it now is. And then I saw that, of course, it had not happened to Frodo at all.