They come into being for a variety of reasons:
many trades and professions have traditional nicknames based on surnames or job. The British Royal Navy, (and other organisations)for example, uses "Spider" Webb,
"Smudge" Smith, "Chalky" White, "Bungy" Edwards, as surname based nicknames, whilst shipwrights and writers are invariably called "Chippy" and "Scribes", even when away from the workplace.
Alleged humour and original wit by the nickname-giver.
These nicknames are intended to be humorous, as the inventors are (wrongly) convinced that nobody has heard them before; the nicknames invariably refer to physical or racial chareacteristics
Lofty or Snowy for any tall person
Specs or Joe90 (a bespectacled heroic puppet from a 1960s TV program) for anyone who wears glasses.
Titch, Shorthouse, for those shorter in height than the nickname giver.
Taff, Jock, etc for Welsh or Scottish folk.
Nastiness: very popular in politics.
Usually invented by the media, but occasionally by political enemies, or worse, by friends.
In the UK parliament we have had
The Beast of Bolsover ( Dennis Skinner), Geoffrey Who (Geoffrey Howe), The Chingford Skinhead (Norman Tebbitt).
Affectionate nicknames often develop between couples; occasionally someone in the limelight will be popular enough to have a nickname coined.
"Cuddly" Dudley Moore (a British comedian and jazz pianist)
The Forces Sweetheart (Vera Lynne)
and millions of Honeybuns, Possums, Hotlips, Bigbums, etc, whose nicknames may return to haunt them if they ever leave the partner who bestowed them.
Need to uniquely identify the owner of a common surname.
This occurs a great deal in areas such as Wales, UK, where a community is likely to have many folks sharing surnames such as Jones, Morgan, Thomas, Davies, etc.
Examples would include "Jones the Phones" , "Tommy Lamps", and "Dai Dan Dwr" (Dai underwater, who used to be a submariner))
Nicknames in Wales, however, are not confined to the bearers of the more popular surnames; it is common to hear someone's first name with a nickname tagged on regardless of the surname. Example " Scottish Bob" (who isn't Scottish but wore a tartan tie once), Honest Don (who doesn't cheat at cards but is such a good player that everyone assumes he does)
A nickname trait that seems to be restricted to Wales is where the first name and surname are similar - Tommy Thomas, David Davies will always be called Tommy Twice and Dai Twice respectively (Dai is short for David)
In Wales, nicknames do not always disappear when the owner dies; the author knows of a number of instances of nicknames being passed down to at least 2 generations; one particular family is still known as the Hong Kong Reeses because the grandfather ran a shop called the Hong Kong Emporium in South Wales at the start of the 1900s!