Query optimization is the process of selecting an efficient execution strategy for retrieving query results from databases, involving steps such as query tree generation, plan generation, and code generation. It employs techniques like heuristic rules and systematic estimation to minimize execution time, disk accesses, and response times. Advantages of query optimization include faster query processing, reduced costs, improved system performance, and efficient resource usage.