Why are nouns in French classified into masculine and feminine and what laws do classification depend on?
In fact the classification is at least 6000–8000 years old and dates back to Proto Indo European (also the ancestral language of English, Hindi, Russian, and many others) where there were at first two "genders" (animate and inanimate) and then three genders: masculine, feminine and neuter.
It's possible that these evolved out of genuine religious belief that the objects had gender, but it's also possible that it has always been more or less arbitrary. You can imagine how this happens. Two words rhyme, say the daughter and the water — so speakers "erroneously" assign them the same gender to water by analogy (more plausible if the word for "the" also changes based on gender). Some old folks complain — the kids' slang these days — it makes no sense! But then they die, and two generations later, no one knows that it was ever said differently (there's no writing, remember). Multiply this effect by thousands of years and you get a completely arbitrary gender system.
In short: languages adapt extremely quickly because the young mind is capable of learning intricate systems. The effects generations later are not always logical. You can find examples of this in every language.
As far as I know the distinction exists from the beginning of the language, because it is derived from latin (which also has m/f classification, and even "neutral").
As to rules, for "most" words the gender comes from the object of the word. Also, some words have a gender from their sense, for example, qualities are mostly feminine. Masculine is used for languages, substantives, most countries, city names in general, materials, "calendar" (months, days, seasons)... Jobs used to be mostly masculine but that's changing progressively.
And (sorry ;-) there are exceptions, even among synonyms, there are even words that can be both (amour for example) !
French is Latin mispronounced by proto-Germans. Both Latin and German have three grammatical genders: masculine, feminine and neutral. Both languages are part of the Indo-European family and derive their gender system from the same root. Given this history, it isn't surprising that proto-French started out with these three genders. Over time, like most Romance language, French largely lost the neuter gender (now present only in a few pronouns, e.g. ceci or ça).
The origin of grammatical gender is not fully known. The current theory on Indo-European is that there were originally two genders, animate (for people and personifications) and inanimate (for objects and abstract concepts), and the animate gender split into feminine and masculine. Many Indo-European languages, in particular most modern Romance languages, saw a merger of masculine and neuter.
Although the feminine/masculine distinction in grammatical gender is likely to have arisen from biological gender (feminine to talk about women, masculine to talk about men), the language has evolved considerably since then. Nowadays, in French (like in most other languages with feminine and masculine genders), masculine and feminine usually match biological gender when applied to people or animals, but carries no implication when applied to other nouns: it's just an arbitrary grammatical feature.
Wikipedia has a list of languages by type of grammatical gender. Most languages are based on a masculine/feminine, animate/inanimate or a variation thereon (common/neuter, rational/non-rational, …). However there are languages with noun categories based on other considerations (some linguists use the term noun class rather than gender in this case). WALS has several maps of languages by gender types: number of genders, sex-based or non-sex-based, semantic or formal. Most languages that have feminine/masculine genders are of the formal type, i.e. the grammatical categories are to a large extent arbitrary when not refering to a person.