As far as I’m aware, it’s unfortunately contextual.
If you have a sentence like
彼は何もわからないように黒板をぼんやり見ていた。
“He was absentmindedly looking at the blackboard as though he didn’t understand anything.”
then it’s probably obvious that ような wouldn’t make sense, because then 彼は何もわからない would be modifying blackboard and that would be quite strange.
But there are cases where both ように and ような can work because the sentence could either be describing the way in which the entire following clause is performed, or it could be modifying just the noun, but that modified noun still makes sense in the clause in which it is contained.
Perhaps something like
彼は嬉しそうに笑顔をしていた。
“He was smiling as though he was happy.”
彼は嬉しそうな笑顔をしていた。
“He was making a happy-seeming smile.”
Pardon the clunky translations. The point is that ように (or in this case, そうに) is an adverb that thus modifies the following verb-containing phrase 笑顔をしていた, describing the manner in which the boy smiles. ような (or そうな here), on the other hand, is an adjective that modifies the following noun 笑顔. The meaning is effectively the same, but grammatically the choice of な vs に indicates what exactly the 嬉しそう is modifying.
Sometimes one or the other will sound better to native speakers, so I’d just recommend taking note of how you see them being used in native material to try and build up native-like intuition over time.