了 grammar is hard. This particle has a lot of uses and it's difficult to pin down anything specific about how and when it's used. You can group 了 usage into four different categories, though. Of these, there are two important ones. Here's a summary of them.
Verb 了 “动词+了”
This kind of 了 is also known as perfective aspect 了, or completed action 了, as it is used to mark these. It appears directly after the verb, before the object.
Verb 了 indicates that the action of a verb is completed in whatever time frame we're talking about. This could be past, present or future. The action is complete relative to what we're talking about, not to the time when we're talking.
Zuótiān wǒ mǎi le liǎng gè mántou.
Yesterday I bought two steamed buns.
Dàole hónglǜdēng yǐhòu, wǎng yòu guǎi.
After you get to the traffic lights, turn right.
Míngtiān wǒ chī le wǔfàn yǐhòu jiù gēn péngyǒu chūqù wán er.
Tomorrow I'll go out with friends after having lunch.
As you can see, 了 does not mark tense. It marks the completeness of an action; this is called aspect. Tense relates to the time of speaking, aspect relates to the time the action takes place. They often overlap, but they're not the same thing.
With quantities 与量词连用
One common use of verb 了 is to mark quantified verbs. So if the object is a specific quantity or duration, verb 了 must be used. Look out for verbs and numbers occurring together; verb 了 will often be there.
Wǒ chī le sān kuài dàngāo.
I ate three pieces of cake.
Wǒ zhǐ shuì le sān gè xiǎoshí jiào.
I only slept for three hours.
Rúguǒ nǐ zhǐ shuì le sān gè xiǎoshí jiào , yīdìng huì kǎoshì kǎo dé bù hǎo.
If you've only slept for three hours, you certainly won't do well in the exam.
Nǐ zuò le yīgè xiǎoshí zuòyè kěyǐ kàn diànshì.
Once you've done your homework for an hour you can watch TV.
Notice how in all of these, the action of the verb is complete. This is always the case because you can't specify the quantity of the action until it's finished. There are quite a few other situations where verb 了 appears, but the main principle of all of them has been outlined above.
Sentence 了 “了”放句末
This 了 also known as modal 了 and change of state 了. Sentence 了 appears at the end of the sentence, hence the name. It's a bit trickier than verb 了.
Sentence 了 indicates that a new situation exists; the state of things has changed. In English, this might be marked with 'now' in positive sentences or 'any more' in negative sentences.
Tiānqì lěng le.
The weather is now cold.
Wǒ bù chōu yān le.
I don't smoke any more.
Tā shì yīshēng le.
She's a doctor now.
Tā huì kāichē le.
He can drive a car now.
Sentence 了 could be thought of as saying 'It is now the case that…' before a sentence. The implication of sentence 了 is that things were not that way before.
The tricky bit is that sentence 了 is also used to indicate the immediate relevance of the statement. Sentence 了 marks out a sentence as being of direct relevance to the current situation. It has a point to make. Unlike verb 了, sentence 了 is always about the situation at hand.
Verb 了 and sentence 了 together “动词+了”以及句末的“了”
Understanding verb 了 and sentence 了 pretty much covers 了 grammar. There is another use of 了 though: a combination of the two.
Sentence 了 and verb 了 can both appear in one sentence, and the implication of this makes sense if you think it through. When verb 了 and sentence 了 both appear in a sentence, it describes what has been completed up till now.
This could be thought of as something like "at this point, it's now the case that x has been done". The equivalent of this in English might be 'so far' or 'up till now'. It's usually translated into the continuous aspect.
Nǐ hē le shíyī píng píjiǔ le!
You've had eleven bottles of beer!
Tā shuì le shí gè xiǎoshí jiào le.
He's been asleep for ten hours now.
Nǐ yǐjīng chīle tài duō le!
You've already eaten too much! [so far] .
One last thing to know is that where verb 了 and sentence 了 occur together and would be right next to each other in the sentence, they merge into one. This single 了 then covers verb 了 and sentence 了, rather than ending the sentence with '了了'.
In these cases, it's not totally obvious whether the 了 is a merged one or not. You just have to decide from context. For example, "我看了" could be "I saw it" or "I've now seen it".
The above three kinds of 了 are what you really need to know about 了 grammar. However, there's one last kind of 了, and it's different to the ones above. It's when 了 is pronounced liǎo rather than le.
Liǎo 了 literally means 'finish', 'complete' or 'achieve'. Liǎo 了 is a verbal complement, and works the same way as 到 as part of the potential complement. It indicates the success or failure of an action, appearing with 得 (positive) or 不 (negative).
Nǐ lái dé liǎo ma?
Can you make it?
Wǒ kàn bù liǎo ā!
I can't see!
And, just to really confuse you, here's a sentence with sentence 了 and liǎo 了 together:
Píngguǒ mǎi bùliǎo le!
You can't buy apples anymore!