The speed of a seismic wave depends on the density and elasticity of the materials through which they travel.
The difference in wave speed has a profound influence on the nature of seismograms. Since the travel time of a wave is equal to the distance the wave has traveled, divided by the average speed the wave moved during the transit, we expect that the fastest waves arrive at a seismometer first. Thus, if we look at a seismogram, we expect to see the first wave to arrive to be a P-wave (the fastest), then the S-wave, and finally, the Love and Rayleigh (the slowest) waves. Although we have neglected differences in the travel path (which correspond to differences in travel distance) and the abundance waves that reverberate within Earth, the overall character is as we have described.
The fact that the waves travel at speeds which depend on the material properties (elastic moduli and density) allows us to use seismic wave observations to investigate the interior structure of the planet. We can look at the travel times, or the travel times and the amplitudes of waves to infer the existence of features within the planet, and this is a active area of seismological research. To understand how we "see" into Earth using vibrations, we must study how waves interact with the rocks that make up Earth.
Several types of interaction between waves and the subsurface geology (i.e. the rocks) are commonly observable on seismograms
Refraction
Reflection
Dispersion
Diffraction
Attenuation
As a wave travels through Earth, the path it takes depends on the velocity. Perhaps you recall from high school a principle called Snell's law, which is the mathematical expression that allows us to determine the path a wave takes as it is transmitted from one rock layer into another. The change in direction depends on the ratio of the wave velocities of the two different rocks.
Refraction has an important affect on waves that travel through Earth. In general, the seismic velocity in Earth increases with depth (there are some important exceptions to this trend) and refraction of waves causes the path followed by body waves to curve upward.
Learn more about Seismic Waves and Earth's Interior at Penn State Earthquake Seismology.