The fact that such a barrier existed was first noticed in experiements by Paul Ehrlich in the late 19th century. Ehrlich was a bacteriologist who was studying staining, used for many studies to make fine structures visible. Some of these dyes, notably the aniline dyes that were then popular, would stain all of the organs of an animal except the brain when injected. At the time Ehrlich attributed this to the brain simply not picking up as much of the dye.
However, in a later experiment in 1913, Edwin Goldmann (one of Ehrlich's students) injected the dye into the spine directly. He found that in this case the brain would become dyed, but the rest of the body remained dye-free. This clearly demonstrated the existence of some sort of barrier between the two sections of the body. At the time it was thought that the blood vessels themselves were responsible for the barrier, as there was no obvious membrane that could be found. It was not until the introduction of the scanning electron microscope to the medical research fields in the 1960s that this could be demonstrated.
In the body, the capillaries (the smallest of the blood vessels) are lined with endothelial cells which contain small gaps between them. This allows chemicals in solution in the body to pass to and from the blood stream, where they can be carried about the body. In the brain these endothelial cells are packed much tighter together, and allow almost nothing to pass in and out. The blood-brain barrier blocks all but the smallest molecules: oxygen, carbon dioxide, and sugars pass with no difficulty, but most drugs are too large to pass the barrier.
The blood-brain barrier appears to exist primarily to protect the brain from the chemical messenger systems flowing around the body. Many bodily functions are controlled via the use of hormones which are detected by receptors on interested cells throughout the body. The hormones are released on cue from the brain, so if they acted on the brain it could cause problems. In addition, the blood-brain barrier is an excellent way to protect the brain from common infection, an infection of the brain is very rare.