Edit: @Luuan and others pointed out in the comments that rather the entire lifecycle of the sandworm instead of the sandworm itself is autotrophic and that sandworms also eat smaller worms and sand plankton.
"Now they had the circular relationship; little maker to pre-spice
mass; little maker to shai-hulud; shai-hulud to scatter the spice upon
which fed microscopic creatures called sand plankton; the sand
plankton, food for shai-hulud, growing, burrowing, becoming little
makers."
That doesn't mean, of course, that they don't also "eat" the sand to get minerals and such. My paragraphs are based on information that was available in the Wiki at the time of my writing. I will leave them in because the answer was already upvoted and accepted. Take these paragraphs with a grain of salt and assume that the truth is somewhere in the middle.
Paragraphs from my original answer:
Sandworms are described as autotrophic animals. That means they produce their own food. Plants are autotrophic, for example. They use photosynthesis as an energy source to convert water and carbon dioxide. The sandworms might employ some sort of chemosynthesis, using chemical reactions. Or they might use a process not actually found in animals on earth and connected to their unique physical attributes.
What the worms feed on is actually the very sand of Arrakis. Organic compounds in the sand are just something they swallow along the way, but not their main food source. They convert minerals and chemicals. Among other things they produce oxygen in the process. They are described in the books as massive oxygen factories.
It should be noted that sandworms don't swallows things from the
surface because they consider it food. That is just the result of an
extreme territorial defense instinct.
Edit:
Most likely both is true, that some things they eat as food and other just to defend their territory.
I think the exact process of how a sandworm processes the sand and whatever else it swallows is never clearly explained. But Frank Herbert has his characters explain that "a sandworm's internal workings resemble that of a massive blast furnace, producing intense heat and flames"(see last paragraph here). The first mention of this was in "Children of Dune" I believe, but I'm rather sure that Leto II also mentions it in "God Emperor of Dune". Leto II explains that this is part of an adaptation to deal with the friction heat the worms generate when they travel through sand.
If the temperatures inside the worm are indeed that high, it stands to reason that it would simply melt down the swallowed harvester.