It doesn't get the character anything or even advance the plot. What separates this trope from a character's other evil or cruel acts is that this bit of evil is gratuitous.
Both devices are used to help the audience become emotionally invested in the story. Conversely, the creator may show a character being kind for no apparent gain, to demonstrate that the character is a nice person and someone the audience is meant to cheer for.
Why this trope works could be expressed in the words of William Cowper: “I would not enter on my list of friends (though graced with polished manners and fine sense, yet wanting sensibility) the man who needlessly sets foot upon a worm.” In other words, a cruel act, no matter how trivial, establishes someone as a cruel person. When a character does something evil, cruel or very mean for no apparent gain, because the author wants to demonstrate that they are not a nice person and shift audience sympathy away from them.