Das . Die . der .
how to know which one is right for a specific word
I think you should learn the nouns together with their articles right from the start. So you should not learn "Baum", but "der Baum", for example.
There are no general rules, in particular for those simple words like "der Baum". But there are several endings that are associated with certain genders.
For example, words ending with the suffix -ung always are feminine: "die Hoffnung" (hope) "die Strömung" (flow), "die Haltung" (attitude) etc.
I think this is a good overview (source: University of Michigan): https://www.lsa.umich.edu/german/hmr/Grammatik/Gender/Gender.html
Learn the word with the "der" die" or "das" instead of learning like "Junge" learn it like "Der Junge" instead of it byitself
There are too many, very usual, counter-examples: der Junge (the boy), der Matrose, das Besondere (the special (thing), and there are many more nouns formed from adjectives this way), der Riese (the giant), der Affe (monkey), der Philologe, der Löwe (lion), der Hase (hare) etc