It is hard to give a general answer. It wasn't invented, as such, but in the Indo-European linguistic family, a distinction was drawn in the mists of time between an 'animate' and an 'inanimate' class (which is perhaps the most important category distinction that there was to be drawn), and it is from that binary division that a three-fold gender system evolved.