In The Lord of the Rings, is there an explanation for where magic comes from? How is it possible in a world of otherwise predictable physical consequences? For example, is it energy or something ethereal that allows Gandalf to work his spells? Something different?
Magic is inherent in the actions of all creatures in Middle-Earth. This comes up a few times in LoTR, especially when the Hobbits are out and about meeting new people. They see "magic" in the everyday actions of the Elves, while overlooking their own "magic" (being able to pass silently, for example).
I found an excerpt online that covers this pretty well: http://ocw.nd.edu/philosophy/ancient-wisdom-and-modern-love/selections-from-the-lord-of-the-rings
“What do you think of Elves now, Sam?” he said. “I asked you the same question once before –it seems a very long while ago; but you have seen more of them since then.”
“I have indeed!” said Sam. “And I reckon there’s Elves and Elves. They’re all elvish enough, but they’re not all the same. Now these folks aren’t wanderers or homeless, and seem a bit nearer to the likes of us: they seem to belong here, even more than Hobbits do in the Shire. Whether they’ve made the land, or the land’s made them, it’s hard to say, if you take my meaning. It’s wonderfully quiet here. Nothing seems to be going on, nobody seems to want it to. If there’s any magic about, it’s right down deep, where I can’t lay my hands on it, in a manner of speaking.”
“You can see and feel it everywhere,” said Frodo.
“Well,” said Sam, “you can’t see nobody working it. No fireworks like poor Gandalf used to show. I wonder we don’t see nothing of the Lord and Lady in all these days. I fancy now that she could do some wonderful things, if she had a mind. I’d dearly love to see some Elf-magic, Mr. Frodo!”
This part of the book leads into Frodo and Sam looking into the Mirror of Galadriel
“And you?’ she said, turning to Sam. “For this is what your folk would call magic, I believe; though I do not understand clearly what they mean; and they seem to use the same word of the deceits of the Enemy. But this, if you will, is the magic of Galadriel. Did you not say that you wished to see Elf-magic?”
The Elves don't really "do" magic. They simply are magical. In a similar way, all of the creatures of Middle-Earth have their extraordinary talents. Magic is everywhere, and so goes unremarked in everyday life.
Gandalf is a kind of special case, in that he's a Maiar, so his magic is a bit more obvious, though he tries not to apply explicit magic to a problem where the right word in the right ear would suffice.
Magic, as one can understand it, comes from The Song.
When Eru Ilúvatar created all things, he did so by making the Ainur sing a song with some themes he gave. The so called first theme contained the Elves, and the second theme the Men. Morgoth was trying to subvert the themes and made some Ainur sing his own compasses instead of those of Ilúvatar.
So everything that happens in Middle Earth is a consequence of the Song, from its start to its end, since the end of the time were in the song as well.
Gandalf is a Maiar -- essentially a divine being. So the magic could be called divine, moreso than magic.