When I was even younger, I thought when you buy a stock, you are lending a company your money to use to develop new stuff, and then over time when they profit from your money their stock should go up and pay you some extra money? Is that how a stock works?
A stock is
equity - meaning ownership. So if the company has 1,000 shares outstanding and you buy 100 shares you own 10% of the company. Buying a companies bonds is lending the company money, just as buying US treasury bills or bonds is lending the federal government money.
The market capitalization is the number of shares outstanding multiplied by the current price. So when you see a fact like 'GE is a $500 billion dollar company', it means that the number of its shares times the market price adds up to $500 billion. That value of course fluctuates as the price increases and decreases.
The company itself will also 'own' shares of its own stock. When the company goes public it typically doesn't sell all of its shares. So the company can make money off of its own stock as the price increases and may sell some from time to time or offer some of the shares as stock options to employees (that's how the excecutives get rich!).
When a company needs more capital they can also create more shares 'out of thin air', just as the federal reserve creates more dollars out of thin air. Doing so dilutes the holdings of existing shareholders because now there are more shares outstanding and earnings per share, a key measure used to value a stock, goes down.
Some stocks pay dividends which are paid out of company earnings and in a sense is like interest on a savings account, but equity stakes depend on the price of the stock increasing to make a profit (unless you are shorting the stock, then the stock must fall to have a profitable trade).
How come we can buy a stock on Monday and get a huge swing in value by Friday?
For valid reasons as well as invalid reasons. If the company announces a huge earnings increase, investors will bid the price of the stock up but sometimes prices get bid up for no good reason at all - like with the tech stock bubble where any company at all was bid up even when that company had never made a dime.
Likewise, investors will bid lower and cause the price to drop if the company has an earnings shortfall, a product failure, announces layoffs, etc.
The key point that many people don't understand is that the stock market is an auction market. The stock is worth what investors will pay at any given time. If a given stock ended the prior day at $100 per share and then bad news is announced (real or perceived), investors might bid only $80 the very next day. If anyone hits that bid, now all shares are worth $80.
Is that because of these derived values, of people shorting stocks assuming it will go down because business is bad, so basically they make money off of betting it goes down? So is buying a stock betting it goes up, and then if more people buy it you 'win'?
Yes, shorting a stock is a bet the price will go down. You borrow the shares from the brokerage and pay the current market price. If the price drops, you then buy them back at the new lower price and your profit is the difference between what you sold them for (when you initiated the short) and the price you paid to buy them back and return the to broker. A put option is another way to do the same thing.
Shorting can be dangerous because your upside is capped (the most the stock can do on the downside is fall to zero) but your downside could be infinite if the price keeps rising. You have to maintain a 'margin', which is typically 50%, so as the price keeps increasing and your margin becomes less than 50%, you get the dreaded 'margin call' you may have heard about. 'Shorts' get a bad rep for driving a stock price down but in reality they serve a purpose. It is the shorts that discover overvalued and/or fraudulent companies and effectively punish them. I've only shorted one time in my life, but I was successful.
Going 'long' on a stock (buying) is a bet the price will increase. If it does and you sell, you pocket the difference between what you paid and what you just sold it for, minus any transaction costs to the broker.