Apparently, it is believed that rocks located in the thickness of the earth, where time flows more slowly under the influence of gravity, are not particularly susceptible to radioactive decay. And as soon as they erupt to the surface, gravity falls, time accelerates and decay begins. Or it is believed that the formation of the solar system began with a supernova explosion and a couple of hundred million years can be neglected.
Starting from the moment when the rock was formed exactly as a rock, it practically ceases to exchange matter with the environment, because the diffusion rate in a solid is very low. Moreover, its initial composition is determined by the chemical properties of its constituent elements. If the chemical properties of the radioactive isotope of interest to us and the final product of its decay are very different (and this is almost always the case), then there was initially a radioactive isotope in the rock, but there was no decay product. Hence the conclusion: in the rock at any moment of time there is exactly as much decay product as the original radioactive isotope decayed (initially it did not exist, it did not come from the outside, the one formed inside the rock did not disappear anywhere). Knowing the half-life and the ratio of the content of the initial and final isotope, we automatically obtain the formation time of the rock.
In practice, of course, everything is somewhat more complicated, but the basic idea is this.