The key to learning to use the indirect object pronouns is the same as the key for direct object pronouns. You must learn to think in phrases, not words. The phrases consist of a pronoun and a conjugated verb. In the following examples, note that the IO remains the same, while the subject of the phrase changes.

no me compra
he doesn't buy (for) me

no me compras
you don't buy (for) me

Remember, the IO pronouns le and les present a special problem because they are ambiguous. That is, they can stand for different things.

le
to (for) him
to (for) her
to (for) you-formal

les
to (for) them
to (for) you-all-formal

The following sentences, while grammatically correct, are ambiguous:

Ella no le escribe una carta.
Ella no les escribe una carta.

Out of context, there is no way we can know the meaning.

Ella no le escribe una carta.
She doesn't write him a letter.
She doesn't write her a letter.
She doesn't write you (formal) a letter.

Ella no les escribe una carta.
She doesn't write them a letter.
She doesn't write you-all (formal) a letter.

Since le and les can mean more than one thing, a prepositional phrase is often added to remove the ambiguity.