In the ancient world spices were frequently traded over great distances. The Middle East for example was an important trading hub for spices that were transported from Asia to Europe. Spices from India like pepper, cinnamon and ginger came to the Romans this way. The Romans used those oriental spices only in small amounts though, instead they preferred spices that came from plants that were native in the mediterranean area. Solely pepper was used in many dishes. The Romans also weren't familiar with sugar, they used honey instead to sweeten their meals.
It is likely that the use of spices in this way continued in the early Middle Ages. During that time monastery gardens started to come up. Monks were growing spice plants that were used to season bland fasting meals. Yet those spices didn't just serve a culinary purpose, they were also used in a medical or spiritual way. Dill, sage, fennel and marjoram were used to cure illnesses and as a charm that was supposed to protect its wearer.
After the time of the Carolingians many native spice plants from gardens and forests diminished in their importance. New and intense spices out of the Orient were used. This was possible because of an intensive trade between Orient and Occident. The Crusades also lead to an intensive cultural exchange between both regions. The merchants of the 10th and 11th century realized that the transport of the space-saving yet expensive spices in their relatively small ships was very profitable. The reason for the high costs of spices were their rarity, the long transportation routes and the use of intermediaries. Especially Asian spices were very expensive. Those were transported from India to the Near East and later to Italy using Venetian ships. Afterwards they were transported overland north to other cities.
Traded goods weren't just the same spices that ancient Europe already knew like cardamom, pepper or cinnamon but also new ones like nutmeg, cloves or galangal. The new spices were used to bake bread that promoted digestion, to improve the taste of wine and preserved meat and to preserve food. Spices were also used as medicine or perfume. Spices that had a pleasant smell gained a religious meaning and people even claimed those spices were connected to paradise. Many people believed paradise was in fact a real place on Earth, they thought it was located somewhere in the Far East which was the same place many spices were coming from. This religious meaning combined with their rarity and expensive price made spices into a status symbol of the wealthy.
The most expensive spice was saffron from the Near East which had already been used as a medicine in the ancient world. During the Middle Ages it became an ingredient in the kitchens and was used to dye meals yellow. The price of saffron was similar to that of gold which is why there were also imitations and fakes. Pepper on the other hand was imported in such large amounts that even the middle class was able to afford it. Another new spice was cane sugar that came to Western Europe during the Crusades. Cane sugar was way more expensive than honey and just as many other oriental spices only accessible to a very small, wealthy part of the population.
The production of honey also wasn't able to satisfy the demand which is why large parts of the populace only rarely had the opportunity to taste sweet meals. Yet ginger, sugar and cinnamon came in increasingly large amounts to Europe and were used to season sweet pastries. The tradition to bake "Lebkuchen" came up: German treats that were often baked by monasteries and handed out to the poor. The German city Nuremberg became famous for them because it was already an important trading hub of spices in Europe. The names of those sweet pastries still carry a hint about their origin. In German they are called "Pfefferkuchen" which means "pepper cake", in English there's "gingerbread".
From the late Middle Ages on the majority of people used spice plants like caraway, dill, parsley and garlic which were grown in local gardens. Besides those many spice plants and medicinal herbs that had been cultivated in monastery gardens were given to the local populace. The trade with the arab world intensified since the 14th century. Next to spices other foods like dates, figs and raisins found their way to Europe. The profit out of the spice trade made many companies and cities very rich and lead to an intense exchange of cultural elements and food traditions between Europe and the Arab world. The trade monopoly of Venice over the much sought-after spices lead to the Spanish and the Portuguese searching for a direct sea route to India as an alternative. Once this was discovered in 1498 the Portugese began to dominate the spice trade.
With the discovery of America new spices found their way to Europe. Some spices that were already known were brought to distant plantations of European colonial powers, others (and the trade monopoly connected to them) were objects of bitter fights that lasted over centuries. The Arab world was less important as a trading hub for spices at this point.
To summarize, spices were an important object of trade that shaped the history of Europe and many other regions over centuries. They brought wealth to many companies, cities and countries and even lead to wars. They were more than a mere way to improve the taste of food, they were used to preserve food, heal illnesses, serve as medicine, perfume, status symbol and religious sign.
First of all, this question depends on the particular region from which demand originates. A highly demanded spice in Europe may have had little value in China, partially because of culinary tastes, and partly due to the relative availability of said spice. However, assuming you are referring generally to Western tradition, some spices that were commonly valued in antiquity were cinnamon, cardamom, turmeric and ginger, and in the Middle Ages and Renaissance became black pepper, ginger, cloves, nutmeg and cinnamon.
By the Middle Ages, black pepper was well-known and available, but priced exorbitantly and controlled by Italy, who dealt almost exclusively with the Muslim middle-men that transported goods from the east. In fact, it is believed that the ability to purchase black pepper directly from the source was the motivation behind finding a sea route to India. Once the route was discovered and European nations established trade with the Spice Islands, they also found access to the other spices I had mentioned. These spices occasionally made their way into Europe, but were even more obscure than black pepper.
These spices are noteworthy because they tend to be seen as being the most desirable and profitable goods in the luxury goods trade. Some mistakenly believe spices were a necessity to food preparation, but as scholars argue, their high prices and status as luxury goods made them unavailable to most of the population, and thus could hardly be considered necessities. Instead, they were generally acquired by the rich as symbols of status, and most importantly, to improve the taste of food. In the modern world, with spices easily available at most stores, their ability to make food enjoyable to eat tends to be taken for granted.
For a comparison of how and why the rich of Europe valued these spices, consider the role of the coltan ore used to make batteries for our electronic products, such as our phones. Coltan is mined primarily in Africa, especially in Rwanda and the DRC, as well as in some areas in Asia and South America - that is to say, it is not mined in North America or Europe. At the same time, our smartphones are not necessities to life in the same way that food is. Despite this, we continue to mine and purchase coltan at high rates because we enjoy the lifestyle benefits and social acceptance that having a smartphone provides. In those days, spices represented the same kind of value.
As an aside, the spice trade actually fell off after the 1660s and the rich stopped demanding spices as they had done in the past. First off, the abuses perpetrated by European powers, such as the Dutch East India Company, on both the peoples and environment of southeast Asia, were unsustainable and unquestionably hurt the trade. At the same time, the chili pepper, newly introduced from the Americas, proved to be much more popular in both Asia and Europe. The final blow came when François Pierre La Varenne published his seminal cookbook Le Cuisinier françois in 1651, establishing the foundations of French cuisine. His style became incredibly popular, especially among the rich, and called for sophistication in preparation and presentation rather than intense flavor. Thus, spices fell out of fashion. In the modern day, globalization has made it possible for the lower classes to also acquire spices, leading to their resurgence as an addition to food to make it more palatable.