Meditation has two effects on this: firstly, it encourages non-thinking (or focusing on some specific thing like breathing), which means less time for random thoughts, which means less time for feeling bad because of random thoughts.
Secondly, it encourages people to reflect on the value judgements their thoughts are making, which makes them more likely to realise the subjectivity in their judgements (due to https://en.wikipedia.org/wiki/Regress_argument), which makes them more likely to update their worldview into one in which fewer judgements are made. Fewer judgements means fewer thoughts cause unhappiness. This is expressed in a quote by Stoic philosopher Marcus Aurelius: "If you are distressed by something external, the pain is not due to the thing itself, but to your estimate of it; and this you have the power to revoke at any moment."; worldview has a huge effect on how much someone suffers, and meditation encourages reflection, and reflection encourages changes in worldview.