From Sonic Retro
G.U.N. (Guardian Units of Nations) is an army in the Sonic the Hedgehog universe. Fifty years before the main events of the series, they were responsible for the shutdown of Space Colony ARK and the deaths of Gerald and Maria Robotnik. In more recent times, G.U.N. is at the forefront of efforts to defend the world from Eggman and alien invaders, led by their capable (if nameless) G.U.N. Commander.
The army believed that Gerald Robotnik's research would be a threat to the planet, and so organized a plan to shut down ARK and destroy Project Shadow. They would invade the ARK under the ploy that there was an accident and do away with anybody that worked on Project Shadow. They then captured the Biolizard and encapsulated it. However the then-finished project, Shadow the Hedgehog, fled in an escape capsule. During the video game Sonic Adventure 2 the G.U.N. army's robots are constant enemies.
The role of GUN in Shadow the Hedgehog's' plot is to repel the invasion of Earth by the forces of the Black Arms by any means necessary - including the (presumingly unwanted) assistance from Sonic the Hedgehog and his friends. During the opening FMV sequence, it is shown that Earth is being overrun, with GUN's forces almost helpless - hence the temporary alliance. It is also strongly hinted during the game and FMV sequences that GUN's mission is the investigation (and possible destruction) of Shadow himself, depending on the player's moral choices within the game. (Their attempts at doing this are mainly caused by the commander of GUN due to his personal hatred of Shadow, although in one ending he realizes Shadow isn't evil.)
GUN's role in Sonic Adventure 2 was also expanded in Shadow the Hedgehog, where a one of the levels takes Shadow and Maria through the colony during the GUN incursion and it also shows Shadow fighting one of the weapons after they harm Maria. This is the only game where GUN was not a villain. It is likely that they, like Dr. Eggman, have merely temporarily allied with Sonic for convenience.
Chain O' Command
G.U.N.'s precise legal status seems somewhat up in the air. At times it is indicated to be something of a "World Police Force" (circa Sonic Riders: Zero Gravity, where a radio announcement in the opening scene calls G.U.N. "the international security organisation"); however, at other times G.U.N. seems simply to be the national military of the United Federation (circa Shadow the Hedgehog (game), where the GUN Commander defers to the UF President). Also, the organisation is conspicuous in its absence when the independent city-state of Soleanna comes under attack from Eggman in Sonic the Hedgehog (2006 game).