Historically, Chinese people had the image of its borders continuously breached by "uncivilized tribes" given to mayhem and destruction. Within the southern parts of China, the term gweilo was used. In northern parts of China, the term "west ocean devil" (西洋鬼子 xiyáng guǐzi) was used, Europe being West of China.
The term gui (鬼) in guizi (鬼子) is an adjective that can be used to express hate and deprecation, an example being the local's expression of their hatred towards the Japanese during their occupation of China in World War II with the same gui (鬼). It conveys a general bad and negative feeling and is a somewhat obsolete and archaic/old-fashioned term nowadays and other more modern terms have largely replaced gui (鬼) for similarly negative meanings.
The character gui (鬼) itself can have negative connotations, even without the word zi (死), for example when it was attached to the Westerners in the term yang guizi (洋鬼子; lit. "Western devils") during Boxer Rebellion, Japanese military in the term guizi bing (鬼子兵; lit. "devil soldiers") during the Second Sino-Japanese War and the Korean military with the term er guizi (二鬼子; lit. "second devil"). However, the same term can also be applied derogatorily to any foreign military which was an enemy to China. In Taiwan, anti-Japanese demonstrators hoisted signs with "Guizi! Get out" (鬼子! 快滾) during the 2012 China anti-Japanese demonstrations.