There are lots of words with multiple definitions. You figure out which one is intended by the context. Yes, it's amusing when a word has two definitions that seem opposite each other, but it's usually clear which is meant from context. If it's not, then the sentence was poorly constructed. Well, sometimes people deliberately construct a sentence using such a word in an ambiguous way for humor value.
If you say, "Alice was filled with vanity after she got a starring role in a movie", clearly we mean pride. If you say, "It was vanity for Alice to suppose that she would ever get a starring role in a movie", clearly we mean futile.
As FumbleFingers notes in a comment, people rarely use the noun "vanity" to mean futility these days. That meaning is largely obsolete. We do still use the adjective "vain" to mean futile. As in, "He made a vain effort to climb the mountain". Most often it's used in the phrase "in vain", as in, "Her attempt to find the lost ring proved to be in vain."
There's a famous Bible quote: Ecclesiastes 1:2, "Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity." If you read that quote out of context, I guess it would be ambiguous. Does he mean that everything is pride, or that everything is worthless? But if you read on, it's clear which: "The sun also ariseth, and the sun goeth down, and hasteth to his place where he arose. The wind goeth toward the south, and turneth about unto the north; it whirleth about continually, and the wind returneth again according to his circuits. All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again." And so on. Clearly the point is that everything just goes around and around and little changes. It's all boring and pointless.