As you can probably guess, there are several ways to determine the etymology of words. Probably the most reliable is to look at the genetic relationships between languages - i.e. we know that a word comes from Latin or Greek because it is preserved in a similar form across several modern languages, and we have historical documents in the original language that also use this word (or a very similar form).
Often, a historical phonological process such as I-mutation (umlaut) is observed for a group of words, which can help to prove that they came from the same language.
The etymology of a word becomes more speculative the further back we go - we can be pretty sure that a word came from Sanskrit, Arabic, Latin, or Greek, because these languages are (relatively) modern and well-documented. However, determining which proto-language a word is descended from can be more controversial.
In summary, historical documentation and phonological similarity are the two main ways to determine a word's etymology.