I just don't understand this phenomenon. A company does well, but not to the exact level that some analyst says, and the stock price falls. But in other cases a company can do badly, but not as bad as someone predicts, and a stock price will go up.

Many are not interested in how well a company is doing in general terms, but how well it will do in the (near) future compared to how it is doing now. So, they spot a trend (or, hope to do so), and use it to make a profit.
They sell shares now at a higher price that that you'll be able to buy them tomorrow it the tred is negative, or buy shares now at a lower price than tomorrow's price of those same shares if the trend is positive.