Medieval cooks used a lot of spices. One often hears that they needed them to kill the off-taste of food that had gone bad, and there was certainly some of that, but spices had far more uses than just covering up unpleasant flavors. (And you wouldn't want to serve actually rotten food anyway--it will make you sick, spice or no spice.)
First and foremost, spices were used to preserve. Salt, which we don't tend to think of as a spice, was the major preservative for meat, besides smoking. Ham was smoked and salted; modern hams, which don't even have to be soaked for a day before cooking, much less have the outer, greenish rind cut off, would have seemed remarkably insipid to medieval people.
(Strictly speaking, spices are the seeds or flower parts of a plant, herbs the leaves or branches, and salt is salt, usually produced by letting seawater evaporate from shallow pans, though there were also highly prized salt springs.)
Salt was far more expensive in the Middle Ages than it is now (now it is mostly mined), especially since a person wants something to perk up a bland vegetarian diet. At table, salt was served in little saucers, usually one for every two people. You dipped salt with your little finger, which had to be kept away from the main dish in order to keep from getting greasy. (This is where the "elegant" touch of eating with your little finger extended comes from.)
Pepper, which we now think of as the black stuff in the "other" shaker (beside the salt), was enormously valuable. Unlike salt, it had to be imported over great distances. It was used primarily to help preserve meat. Its value was such that it was priced by the individual peppercorn. People buying pepper would have it floated, to make sure no tiny mud balls had been added to the mix.
Besides preserving food and making that big dish of lentils palatable, spices were also used to show off one's wealth. Anyone who could afford spices used them as a form of display. Spices also made it possible to drink wine all year round--once it started to turn to vinegar, one just stirred in more spice. Modern bottling of wine was only invented in the post-medieval period (as was distilling of spirits), so by summer the wine barrels did not seem as enticing as they had earlier. The wealthy who could afford the wine kept on drinking it, heavily spiced, even as they longed for the new vintage's arrival; the poor stuck with beer.
The spices came mostly from southeast Asia, referred to generically as "India." It was a very long journey, across the Indian Ocean on Arabic dhows, then up to the Middle East, where they were picked up by Italian merchants and brought to western Europe. The spice routes were somewhat conflated in the European imagination with the silk road, that brought silk from China, because both ended up in the Middle East. When Columbus accidentally discovered the Americas, he was in fact looking for a shortcut to the spice islands.
Click here for more on the medieval diet.