Hi! This is a great question. I will cover just a few basic meanings, but it actually could be a great dissertation topic:
за is used when you talk about the purpose, you need the Instrumental case
идти за хлебом (instr) - go for bread
посылать за врачом (instr) - send for a doctor
для is used when you talk about a receiver, you need the Genitive case
идти за хлебом для моего друга (gen.) - go for bread for my friend ("friend" is a receiver)
посылать за врачом для моего сына (gen.) send for a doctor for my son ("son" is a receiver)
Regarding you previous question about "books for children", this is the same idea and the correct phrase is "книги для детей".
You also use за when talk about a cause, you need the Accusative case
сидеть в тюрьме за убийство (acc.) - to serve time in jail for a murder
пострадать за веру (acc.) - to pay the price for defending religious freedom
уважать за возраст (acc.) - to respect for age
One more case - the means:
покупать за деньги (acc) - buy for money. Here "деньги" is the means, not a receiver.
за счёт средств федерального бюджета - for federal budget resources
A different English preposition, but the same idea ("hold" someone "using" his/her body part):
взять за руку - take by the hand
держать за горло - have by the throat
You also use за to talk about a time frame, you need the Accusative case:
выучить русский язык за год (acc.) - to learn Russian in one year
за минуту (acc.) до подачи - one minute before serving
Note that there is also a locative meaning of the preposition за, and a lot of other metaphorical meanings too.
Hope I did not confuse you too much.