Some metals, typically the unreactive ones like gold, are found in the Earth’s crust as pure elements. But most metals are found combined with other elements to form compounds. These compounds are mixed in with other types of rock.
Different metals are extracted from their ores using different methods depending on their position in the reactivity series.
Electrolysis of molten compounds is used to extract the most reactive metals. In principle, all metals can be extracted using electrolysis but it is expensive.
If a metal is less reactive than carbon, it is cheaper to extract it by heating with carbon.
The very least reactive metals, such as gold, occur as pure elements, but may be contaminated with other elements. They are cleaned through various chemical reactions.