This Japanese word is a slightly more polite form of the word gaijin. Both words essentially mean the same thing, that is, "foreigner," but there is a subtle difference best understood by looking at the kanji that make up the words.
Gai means outside or external.
Koku means nation.
Jin is a suffix denoting person or people.
Thus, gaikokujin translates as "external country person," whereas a close translation of "gaijin" would be "outsider." Don't think that this is just paranoid ramblings; the Japanese language relies heavily on levels of politeness, and speakers who wish to be truly polite will almost always say gaikokujin.
Of course, like many derrisive words in English, gaijin has been sort of reclaimed by gaijin, and friends, members of your in-group and other gaikokujin may use it as a sign of closeness.