While FIFA Laws dictate that goalkeepers must wear kits that are easily distinguishable, the origins of why goalkeepers wear different coloured jersey traces back as far as 1909.
When the game of football began evolving, the teams in 1870s are easily distinguishable by the colours of their caps and socks. by 1872, teams have adopted team colours. Prior to WW I, the goalkeeper can only be distinguished from his teammates by the cap he wore. Although according the source below, Scottish goalkeepers were already wearing different coloured kits from the rest of their teammates.
As the goalkeeper is the only player allowed to use his hands in the box in football, you can imagine back then how difficult it is to officiate a match with a goalkeeper wearing the same coloured kit. The confusion must be terrible.
And so FIFA found it logical to legislate different coloured goalkeeper kits into its Laws of the Game if it were to govern matches better.