There are a few reasons why words get accents:
The word does not follow natural stress.
The problem you'll find with this rule is that you either need to know a word's pronunciation to know how to accent it, or how the word's written to pronounce it. It's a bit of a catch 22. But basically, when a word has irregular stress, it's accented. The regular or natural stress for words without written accents is the following:1
- Words that end in a vowel, n, or s are stressed on the next-to-the-last (penultimate) syllable and are termed graves or llanas.
- All others are stressed on the last (ultimate) syllable and are termed agudas.
Using that knowledge, you can work backwards if you know how a word is said. If it's stressed on the next-to-thenext-to-the-last (antepenultimate) or the previous (preantepenultimate?) then it always gets an accent because natural stress is never on those.
All interrogative (question) words are accented. This is a limited list: cómo, cuál, cuándo, cuánto/a(s), cúyo, dó, dónde, qué, quién. When used as a relative pronoun, however, these words are not accented: ¿Dónde está el perro? El perro estará por donde esté su dueño
Disambiguation of words distinguished by prosodic stress
While you might think that te and té are pronounced identically, the truth is they aren't: té always receives a prosodic stress, whereas te will always join up with another word. That is, whenever you say el té, té will be given its own stress within the context of your sentence. te will not. te lo dije might as well be written telodije for how it's pronounced. These words include se/sé, de/dé, among others.
Prevention of hiatus
The Spanish system of diphthong and hiatus is very simple. There are two types of vowels: strong (a, e, o) and weak (i, u). Whenever there are two strong vowels next to each other, each is pronounced in its own syllable (this is called hiatus). When a strong and a weak combine, the weak vowel is instead realized as a semivocal2. Sometimes, however, the weak vowel needs to be “upgraded” to a full vowel in its own syllable. In this case, an accent is often needed to demonstrate its full vowel. Hence both secretaria and secretaría are palabras llanas, but in one, the ia form a single syllable, so the penultimate is the ta syllable. In the second, the i is separated from the a, and this is represented with the accent which nicely also has the stress.
There is a slight limitation within the Spanish orthographic system in that, unless the upgraded weak vowel gets the stress, it's impossible to mark whether two vowels diphthongize or not. Hence, even though enviar looks to be two syllables, the i is really a hidden “upgraded” vowel that exists on its own syllable. But when you conjugate, it becomes possible to mark the hidden hiatus: envío because the i now gets the stress and can receive the written accent. In other conjugations like enviaba, the first a gets the full stress, so you just have to know that the i is separate. In poetry, you can force the two-syllable pronunciation by adding a dieresis on the weak vowel: agüa is á-gu-a, but this rare and mostly done on words that normally diphthongize.
Thankfully, if you know a word is pronounced with, for example, an i-a ending, you can know the i takes the accent. With verbs, that's going to require a detailed enough answer to deserve its own question.
Disambiguation of demonstratives and solo (deprecated)
This rule is no longer in play after the 2010 Orthography. But prior to that, a demonstrative noun would be distinguished from an adjective by placing a written accent on it. Likewise, solo, when meaning alone, went without an accent, while solo meaning solamente was given one.
Disambiguation of preterite ir/ser (deprecated)
It used to be that the preterite forms of these two were distinguished. I can't even remeber off the top of my head which one got the accents. Also, monosyllabic verbs forms used to get accents even though there was no difference in pronunciation. That's thankfully stopped.
Marking of the single-letter words a, o, and abbreviation of allí (deprecated)
In the olden days, for some crazy reason, the preposition a and the coordinator o were both written always with an accent. Then o only got it between numbers: 100 ó 200. Now neither get an accent in circumstance. Going further back, ý was an abbreviation for allí, but now after years of disuse, ý is seen again in a handful of names like Comýn, Ýñigo, or Monteserýn.
1. Knowing this you can work backwards. If a word is said with the stress on the next-to-the-next-to-the-last (antepenultimate) syllable, that is, esdrújula, or the next-to-the-next-to-the-next-to-the-last (preantepenultimate) syllable, that is, sobresdrújula, it necessarily must carry an accent, as natural stress never lands on the (pre)antepenultimate syllables. If a word is grave/llana (next-to-the-last or pentultimate syllable stressed), it will need an accent if it ends in anything other than a vowel, n, or s. If a word is aguda (last or ultimate syllable stressed), then it will need an accent if it ends in a vowel, n, or s.
2. Some might prefer semiconsonant, depending on whether the weak vowel comes before or after. It's mainly convention as the difference between the two is basically none.