The French words inherit their genders from Latin, a language that all Romance languages (English, French, Spanish, Italian, to name a few) originate from. The basis of Latin in all these languages is due to Rome’s world dominance beginning well over a thousand years ago. But gender in words goes much further back to languages several thousand years older than Latin.
Your question about “why would people give these words gender” is a good question that even modern researchers can only speculate on. But if you are really interested in the history of words there is a science dedicated to that called “etymology”. You should look into that!