There are a few reasons:
It's slang, a derogatory word that we have taken ownership of.
Like the "n-word", we can use it if we choose, within our community, but those outside cannot, without fear (or the intention) of causing offense.
"Gaijin" means, for those who live here, "A foreigner living in Japan" as opposed to a "Gwailo" (鬼佬) which is the same thing, but for Hong Kong.
Both Gwailo and Gaijin have a negative connotation, but are often used innocently and with affection by Japanese when referring to foreign friends. When the sweet old lady at the store calls you a gaijin, it's like an American old lady referring to a black person as a "colored person." They don't use the term with malice, so it's hard to get mad. When a cop makes the same errors, it's different...
(The proper, official term is "Gaikokujin" (外国人) )
It feels unnatural to refer to yourself as a "foreigner", so using a different word can be easier. It rolls off the tongue easier, as well.
It helps distinguish those who live here from the tourists—we have our "Gaijin Cards" after all. (The "Gaikokujin torokushou" is the registration card we must carry at all times.)
Like the "n-word," we can use it in a derogatory fashion:
"Don't act like such a stupid, drunken gaijin..."
That said, there are some people who despise the use of the word, even casually. I have one gaijin friend who gets furious every time he hears the word.
We use a lot of Japanese words mixed in with English. Here's an example of a typical sentence from normal conversation between foreign residents:
"Let's meet at Hachiko at eight and then head to the izakaya. If you don't see me, call my keitai. Bill's working late at his kaisha, but he'll probably show up for the nijikai. We'll probably head to the other place for takoyaki and a couple of nama's."
After a while, they don't even feel like foreign words.
Edit: Contrary to what someone else said, the word Gaijin certainly is used negatively by Japanese. It's on the list of prohibited words for print and broadcast media (http://monoroch.net/gallery/kinshi/) as well as government.
Ignorance doesn't make it acceptable.
Edit 2:
The argument that foreigners are responsible is ridiculous. It's a pejorative term and emphasizes Japanese ethnocentrism. Of course we are offended when it is used by Japanese about us.
Japanese people know it is offensive to us, but some use it anyway. They do this because they know the effect it has upon the listener.
Who made it offensive is irrelevant. It's offensive and shouldn't be used by Japanese when talking about foreigners.
BTW, the word "Jap" was originally not offensive either. It was 1880's British slang for anything Japanese. Does that mean I should feel free to use it?