When we look closely at a comet, there is a tiny nucleus, the solid body that constitutes the comet everywhere except when it's very close to the Sun. The icy nucleus is typically a few kilometers across-but when it comes close to the Sun, the icy nucleus outgasses mainly water vapor and produces the coma and a long and lovely tail.
What's happened to that stuff over the last 4.6 billion years?
The Earth formed from the collapse of lumps of matter of the sort condensing from the solar nebula. Therefore in its final stages of formation, it was collecting objects that collided at high velocity and produced a set of catastrophic events, including the melting of much of the surface. This, it turns out, was not a good environment for the origin of life, as you might have suspected. But after a while, when the final sweeping up of the debris in the solar system was more or less completed, water delivered from the outside or outgassed from the inside started forming on the surface, filling in the ancient impact craters. And a trickle of material was still falling in from space. At the same time, electrical discharges and ultraviolet light from the Sun and other energy sources produced in digenous organic matter.