Opponent-process theory explains this. Here's an excerpt from the opponent process page on Wikipedia (different page than the opponent-process theory for some reason):
Besides the cones, which detect light entering the eye, the biological basis of the opponent theory involves two other types of cells: bipolar cells, and ganglion cells. Information from the cones is passed to the bipolar cells in the retina, which may be the cells in the opponent process that transform the information from cones. The information is then passed to ganglion cells, of which there are two major classes: magnocellular, or large-cell layers, and parvocellular[disambiguation needed], or small-cell layers. Parvocellular cells, or P cells, handle the majority of information about color, and fall into two groups: one that processes information about differences between firing of L and M cones, and one that processes differences between S cones and a combined signal from both L and M cones. The first subtype of cells are responsible for processing red–green differences, and the second process blue–yellow differences. P cells also transmit information about intensity of light (how much of it there is) due to their receptive fields.
This Wikipedia page needs a little work, but it already knows more than I do about the wiring. Basically, in effect, strong and prolonged stimulation of one kind of cone (e.g., the red cone) will gradually build a visual signal of the opposite color (blue in this example). When the (red) stimulus is removed, the opposing signal begins to fade, but remains long enough to produce an opposite-color (blue) illusion or afterimage. Lots of fun optical illusions are based on this principle. It works with all colors.
Here's my favorite example for intro psych classes...stare at the black dot in the center and enjoy!
Not sure what the original source for this is BTW. It comes up all over the place with a Google image search, but I haven't found a proper citation yet.