The rules for pronouncing words in Tolkien’s languages are laid out in The Lord of the Rings, Appendix E. Stress, in particular, works like this (for both Quenya and Sindarin):
By default, words are stressed on the third-to-last syllable. (This doesn’t mean that the third-to-last syllable is usually the stressed one – the below exceptions are not uncommon – merely that this is where the stress goes if no exception applies.) For example: Aragorn, Boromir, Denethor, Galadriel, Lothlórien, Undómiel.
The stress falls on the second-to-last syllable if that contains:
A long vowel, marked with an acute accent (á, é, í, ó, ú). For example: Elentári, Cuiviénen, Celebrían, Pelóri, Andúril.
Note that the acute accent does not mark a stressed syllable per se, and there are words where it occurs in an unstressed syllable: Nauglamír, palantír (plural palantíri). That said, if a word features an accute accent, it is very likely to be on the stressed syllable.
A diphthong. For example: Sauron, Gwaihir, Crissaegrim, Finduilas, Mitheithel, Nenuial.
The vowel pairs that form diphthongs vary by language: in Quenya, the diphthongs are ui/oi/ai and iu/eu/au, whereas in Sindarin they are ae, ai, ei, oe, ui, and au. Put differently, ui, ai and au are always diphthongs, oi, iu and eu are only diphthongs in Quenya, and ae, ei and oe are only diphthongs in Sindarin. That said, the latter six pairs seem to be rare in the language where they’re not a diphthong.
Vowel pairs which aren’t diphthongs are sometimes distinguished with a trema, as in Fëanor, Namárië, Eönwë; but this is not always done, as in Moria, Ithilien, Eriador, Osgiliath, all of which feature nondiphthongal vowel pairs and are not usually written with a trema.
A vowel followed by two or more consonants. For example: Mithrandir, Elendil, Isildur, Thorondor, Utumno.
This includes double consonants, which represent a longer version of the same consonant, for example: Morannon, Pelennor, Elessar, Bragollach.
Note that ch, dh, th count as single consonants here; therefore Caradhras, Falathrim, but Forochel, Galadhon, Denethor.
If there are less than three syllables, the first syllable is stressed, as in Amon Amarth, Grond.
For the division of words into syllables themselves, two aspects are worth noting:
y is a consonant in Quenya, but a vowel in Sindarin. Thus, in Quenya Calacirya, stress falls on the cir syllable because its vowel is followed by two consonants (r and y), whereas in Sindarin the y would be a vowel and therefore its own syllable (though the stress would still fall on the cir syllable, since it would then be the third-to-last syllable). On the other hand, a name like Emyn Muil can only be Sindarin, since in Quenya the myn would be an unpronounceable jumble of three consonants.
Also, in Quenya the sequence qu represents two consonants (cw), and the u therefore does not constitute a syllable of its own. However, a qu- syllable is often followed by another, and so it is rare that these two consonants actually make a syllable stressed that wouldn’t be stressed anyways (as the third-to-last syllable); two examples are ataquë, otoquë. (In Sindarin, qu does not occur.)
Finally, keep in mind that not all names in Tolkien’s works follow these rules. For instance, the Elvish rules would mandate that Isengard is stressed Isengard, since the e is followed by two consonants; however, the word is actually of English form, translated from the language of the Rohirrim, and therefore stressed as would be more natural in English: Isengard. (Elvish pronunciation would also dictate that the i is that of “machine”, rather than that of “iron”.)
Appendix E doesn’t directly say how the stress rules apply to hyphenated words, which are uncommon but not completely unheard of. Maybe each part should be analysed separately, or maybe the whole word should be analysed as if the hyphens weren’t there at all.
The strongest hint in the text is probably the poem “Gil-galad was an Elven-king”, which begins like this:
Gil-galad was an Elven-king.
Of him the harpers sadly sing:
The metre here strongly suggests a stress on the first syllable of galad, which is consistent with the interpretation that each part should be analysed separately: then, that syllable would be stressed as the first syllable of its part. (Otherwise, the stress would fall on Gil.)
Personally, I did not find this wholly convincing – I had gotten accustomed to stressing Gil-galad on the first syllable, and was reluctant to give that up. What won me over was the realization of what each interpretation meant for another word, Barad-dûr: if the whole word should be analysed without regard to the hyphens, then the stress there must fall on the second syllable, since it would then be followed by a double consonant: Barad-dûr. That sounds utterly absurd to me, and so I accepted that each part should be analysed separately, yielding Gil-galad (with the first stress being secondary or weaker) and Barad-dûr. Consequently, that is the rule which this tool applies.
In both Imladris and Nargothrond, the second-to-last vowel is followed by two consonants, and so Appendix E dictates that they should be stressed on the second-to-last syllable: Imladris, Nargothrond. However, several poems suggest the stress should actually be on the first syllable:
Seek for the Sword that was broken:
In Imladris it dwells;
The world was fair, the mountains tall,
In Elder Days before the fall
Of mighty kings in Nargothrond
And Gondolin, who now beyond
The Western Seas have passed away:
The world was fair in Durin’s Day.
Others have noticed this before, and some have suggested a solution for this conundrum: Sindarin might feature a rule similar to one found in Latin, sometimes called muta cum liquida, where certain combinations of two consonants really count as one, both for syllabification and for stress: Imladris and Nargothrond, rather than Imladris and Nargothrond. Concerning Stress Placement In Sindarin is an accessible explanation of this rule and also links to other discussions about it.
For the purposes of this tool, the important thing to note is that the rule, even if we accept its existence, does not seem to be applied systematically. For example, consider Gandalf’s words before the West-gate of Moria:
Annon edhellen, edro hi ammen!
Fennas nogothrim, lasto beth lammen!
Here, nogothrim is clearly meant to be stressed on the second syllable, as Appendix E dictates, even though it contains the same othr sequence which in Nargothrond we are led to believe does not make the o stressed. For that reason, this tool does not attempt to guess when the muta cum liquida rule might apply or not, and if you ask it to analyse Imladris and Nargothrond, it will tell you that, in accordance with Appendix E, they should be stressed Imladris, Nargothrond. Whether you believe that, or allow for the muta cum liquida rule, is ultimately up to you.