I think the best way to explain this is with some examples
- 国のために戦う ➝ Fight for one's country
- チケットを買うために3時間並んだ ➝ I lined up for three hours to buy tickets.
- 子供のための公演 ➝ A performance for children
"ため" is usually used in the context of someone doing something for someone else, or doing something in order to accomplish something else.
- 再発しないように頑張ります ➝ I'll try not to let it happen again
- 風邪を引かないようにちゃんと寝てください ➝ Get some sleep so you don't catch a cold.
"ように" is more indirect, often in the context of trying to do something as opposed to a direct cause and effect.
- TOEIC満点を取るのにやるべきこと ➝ Things you should do to get a perfect score on TOEIC
- 大学に入るのに勉強の才能が必要だ ➝ You need to be good at studying in order to get into university.
"のに" usually is used when saying you need X in order to do Y (YするのにXが必要 or YするのにXしないといけない).
I'm not a Japanese teacher or anything so let me know if that's hard to understand (^_^).