Imperialism happened for many reasons. I'm going to explore some reasons why western European nations engaged in imperialism in the 19th century.
One motivating factor for imperialism was a desire for natural resources. By the mid- to late-1800s, many Central- and South-American colonies had claimed their independence (for Spanish America, this mainly occurred between 1810 and 1824). Desperate to continue making the profits they had from the 15th to 19th centuries, European core nations turned their attentions to other regions, and Africa was one such place. Prior to the new technologies of the Industrial Revolution, including breakthroughs in medicine, Europeans were unable to explore too much of the interior of Africa. Armed with new technologies and inoculations, Europeans began to explore Africa. In this process, they uncovered more natural resources and began the process of imperialism.
Another motivating factor for imperialism was the desire for strategic military bases and ports. From the beginning of the early modern period, European nations were in competition with each other for land, resources, and wealth. During the height of mercantilism, European nations believed there was a finite amount of wealth in the world, and they all wanted to have the largest share. Piracy and wars between nations broke out over land; one example was the Seven Years' War between France and Britain over territory in North America and South Asia. During the nineteenth century, this competition continued, and as European nations clamored for land, there was a strategic consideration. Two areas of Africa were excellent ports or choke points that European nations fought over: the Cape of Good Hope in South Africa and the Suez Canal in Egypt. Both of these locations encouraged European nations to engage in imperialism in order to control land and wield power.
A final reason that explains why nations engaged in imperialism in the 19th century was to "westernize" the world. After exploring and learning more about the world, some groups in western Europe felt it was their duty to bring Western values, Christianity, and modernization to the rest of the world. While their aim was helpful in theory, it made dangerous assumptions that the world needs to be run like the West, and therefore assumed that all non-Western countries were primitive. This is well exemplified in Rudyard Kipling's poem "The White Man's Burden," originally written about American imperialism in the Philippines:
Take up the White Man's burden–
Send forth the best ye breed–
Go bind your sons to exile
To serve your captives' need;
To wait in heavy harness
On fluttered folk and wild–
Your new-caught, sullen peoples,
Half-devil and half-child.
To westernize the non-Europeans was seen as the "White Man's Burden," and European nations must send their best to bring modernization and Christianity to the newly conquered, wild, non-Christian, childlike populations of the colonies. Again, these assumptions about what constitutes modernization catapulted Western Europe to new power, while the rest of the world was labeled as "backwards".