Interstellar space is not a perfect vacuum. The mean density in interstellar space in our part of the galaxy is about 1 particle (mostly hydrogen atoms) every 0.6 cubic inch (10 cubic centimeters). Amid this gas is a smattering of dust grains; on average, 1 percent of the interstellar mass is in the form of solid silicate or carbonate grains. Astronomer Robert Trumpler showed in 1930 that interstellar gas and dust absorbs light at a typical rate of 2 magnitudes per kiloparsec (3,262 light-years).
All gas and dust in the interstellar medium absorbs (or scatters) light that passes through it, resulting in the extinction of light from background stars. Most of the extinction at optical wavelengths is due to dust grains, which have typical sizes of 0.01 to 1 micron. A photon with a wavelength smaller than the size of the dust grain can be physically absorbed by the grain, heating the grain up. Longer wavelengths of light can diffract (which causes the light to bend or spread) around the grain.
Optical light (about 400 to 700 nanometer wavelengths) is strongly dimmed by these dust grains. The amount of dimming is proportional to the cross sectional area of the grains times the number of grains in the line of sight. The size of the grain affects the wavelength of light that will be dimmed. Extinction is strongest at short wavelengths and its effects decrease with increasing wavelength.
William Herschel noted the apparent lack of stars in certain directions back in the 18th century, and may be the discoverer of dark nebulae (although he didn’t understand what they were). These dark, or absorption, nebulae are localized enhancements in the density of the interstellar medium by factors of 1,000 to 100,000. A density enhancement of 10,000 means a 1-magnitude attenuation occurs over a fraction of a light-year.
To tell which stars are in the background, look at their colors. Stars whose light passes through the absorption nebula will be reddened — their blue light will be preferentially absorbed and scattered. Be careful though — some stars are naturally red. You really need to have a spectrum (or spectral type) that will tell you what the colors of the star should be.