"Mandarin" is what linguists call an exonym, an external name for a place, people, language or, in this context, a Chinese official.
The word dates back to the 16th century when Portuguese explorers were among the first Europeans to reach China. The Portuguese called the Ming officials they met mandarim, which comes from menteri in Malay and, before that, mantri in Sanskrit, both of which mean "minister" or "counselor", a natural logical progression since they were simultaneously colonizing Malacca on the Malay peninsula.
To the Portuguese and later other westerners, a mandarin was pretty much any official who held a position of power in the Chinese bureaucracy. The mandarins were typically Han Chinese literati, highly educated and well-versed in Confucian philosophy and classical Chinese literature. They were responsible for managing the affairs of the government and overseeing the administration of the country.
While the actual Manchu rulers of China were deeply involved in the governance of the country, their direct participation in the daily management of the country was limited by the size and complexity of the bureaucracy they presided over.
The imperial examination system, which was inherited from the earlier dynasties but refined during the Ming Dynasty, was used to select candidates for government positions based on their knowledge of Confucian classics and ability to write essays in classical Chinese style, and was open to all.