The query optimizer is the component of a database management system that attempts to determine the most efficient way to execute a query.
The optimizer considers possible query plans for a given query, and attempts to determine which of those plans will be the most efficient (or, more usually, "efficient enough"). Statistics about data in tables accessed by the query can be used to determine things such as join type, index usage, etc. Information available to the optimizer may include estimated number of rows in the table, the range and distribution of values in each column, and details about the operating environment (such as disk speed, and memory available).
For more details, see the Wikipedia article.