All the heavier elements that form Earth were made in first and second generation stars that went supernova. That’s a pretty chaotic process and you’d think it would mix things up pretty good. And yet we find gold atoms clumped together in nuggets and veins of silver to mine and lumps of rock that are rich in aluminum or iron or uranium.
Why do elements clump up like this?
Well, looking from the scale of a stellar event even the largest nuggets are still “mixed up pretty good.”
Be that as it may, the physical processes which create the rocks of the Earth’s crust will tend to create local concentrations of given elements or compounds. Just as if when you shake a jar of randomly-sized rocks, sand, and gravel the largest items work their way preferentially to the top. Continuing stirring of a mass of mixed materials will often cause sorting.
There are many processes that go on inside the earth that concentrate the elements in veins. Rock undergoes heating and cooling processes that allowed elements with different melting points to gather together.