The five aggregates are conditioned things. They are impermanent. Clinging to impermanent things will result in suffering.
But that's not all. The five aggregates are not all that they seem to be. Why does a person crave a beautiful form of a human being and is sexually attracted to it? Craving is a habit of objectification-classification aka reification (papanca) - see this answer.
If you see the form of a human being as an impermanent lump of flesh and bones as prescribed in the asubha contemplation, then this type of craving will be diminished.
The foam analogysimile is a generalization of asubha contemplation to cover all the five aggregates.
But how does this relate to Nagarjuna's Madhyamaka? According to Madhyamaka, all things are empty of intrinsic essence (svabhava).
The beautiful form of a human being, is such a thing, which is empty of intrinsic essence i.e. it is empty of the essence given to it by your mind. If papanca is like baking, then svabhava seems to be the cake.
In other words, Nagarjuna is talking about the mental concept of things, and further, he generalizes to cover everything, even physical objects and Nirvana. If you see that Nagarjuna is talking about the mental concept of things, then you would find that he is not contradicting the Buddha.
To my understanding, Nagarjuna is not saying that the world is an illusion, rather he is saying that your mental model of the world is an illusion. To think about anything in the world, you actually need to have a mental model of it.
This can be further linked to anatta. Objectification-classification (papanca) assigns intrinsic essence (svabhava) to things, relative to its relationship to the mental idea of the self (atta), which itself has intrinsic essence (svabhava), given to it by objectification-classification (papanca). According to Snp 4.14, when you put an end to "I am the thinker", the root of objectification-classification (papanca), you will end craving and clinging. And by this, you end suffering.