There is matter spread all through the Universe, it is just spread very, very, very, very thin. The average density of gas in our Milky Way galaxy is about one atom per cubic centimeter. This is a much better vacuum than is obtained in a laboratory, but when integrated over the Galaxy, comes out to quite a lot of mass. This gas is mostly hydrogen (~90%), and helium (~9%), and less than one percent everything else. The gas between galaxies is even thinner, but there is probably something there (it hasn't been measured, though). These elements are in the Earth because they were present when the gas cloud that formed our solar system collapsed to form the Sun and the planets.
NASA's Cosmicopia -- Ask Us - Space Physics - Matter in Space