You dont sell shares you don't have!
You borrow them from someone that does have them. Then you sell them at the higher price to someone else expecting to buy them back at a lower price.
Then you return the shares to the person that lent them to you and the difference is your profit. People would only do this if they perceive the company to be relatively over valued and people are willing to buy.
In both buy cases, the money is made available to the company. The person that originally lent them to you gets his shares back and someone else has become a shareholder, albeit at a more expensive price.
The company may have a lower market cap at this point but if the share price reflects that the company is undervalued, then people will find value in that and buy the shares cheaper. Which raises more money for the company.
The guy in the middle, doing the buying and selling is taking risk, providing liquidity and is helping to facilitating a more stable and fair market. People buy items cheaper from one place and sell them more expensive somewhere else all the time. If you dont know what you are doing, you can lose money, but who's fault is that?
There is always risk, in business or investing but the stock market and capitalism allows your average person to become part owner in business that they would never think about or even be capable of starting up. Investing in stocks and shares of companies is a vehicle that can potentially create wealth for the average person.
How is this bad again?