There are many rules and exceptions on this point, which are founded on the notion that language rules can be formulated, like mathematics, on simple “If X, then Y” principles that always apply—such as, “If it ends with ah, then it must be feminine.”
But language is not like mathematics: the real determiner of any aspect of it (in any language) is what sounds right to native speakers and to anyone with an “ear” for the language. For example, the Hebrew word lailah (“night”) is masculine because lailah tov (“good night”) sounds OK, whereas lailah tovah sounds odd—and the reason for that is probably because all day-related terms, such as boqer (morning), yom (day), erev (evening) and even shavua (week) are masculine. So there is logic, but it is more subtle than a simplistic “If X, then Y.”
Sometimes, the “sounds right” rule defies even the basic rule that the gender of a word is the same for singular and for plural. For example, try this fun test: go up to any native Israeli and ask them quickly to tell you how to say “at the next road junction” in Hebrew. In 95% of cases, they’ll say batzomet haba’ah—because most words that end with o_et (e.g., shoqet, tisroqet, tikhtovet, etc.) are feminine. In fact, only real Hebrew cognoscenti will say batzomet haba—but only after overruling their natural tendency, and in the self-conscious knowledge that it sounds affected. But ask any native Hebrew speaker how to say “at the next junctions”, and they will ALL tell you batzmatim haba’im—which suggests that the word is masculine.
So the feminine version sounds right in singular, but the masculine one sounds right in plural. Try making a rule from that. (My wife, who is a native speaker, actually argues that that’s the rule: “It’s feminine in singular, but masculine in plural.”)
The “sound right” rule defies “logical” consistency in other areas, as well, such as binyanim. For example, in the past tense, a woman might say raziti (“I’ve lost weight”)—which suggests that it is binyan pa’al. But ask her to say “I am losing weight” in the present tense, and she will never say ani rozah (as the binyan pa’al would indicate), but ani marzah—which is binyan hiph’il.
So ignore the rules, and trust your “ear” for Hebrew. You may be pleasantly surprised. You don’t need to be German to realise that the German word boot (“boat”) is feminine, because das boot sounds OK, but der boot doesn’t. The same is true for Hebrew, and indeed, any language.
*
(Originally written in reply to a question at Quora.com).