Let's say I spent 10k and bought some shares of stock A, where does the cash 10k go? Does it go to the stock broker and then the stock broker gives the money to the company of the stock A?
When you buy a share of stock, you are almost always buying from someone who previously purchased that share and now wants to sell it. The money -- minus broker's fee -- goes to that other investor, which may be a person, a company (rarely the company that issued the stock, but that will occasionally be the case), an investment fund, the "market maker" for that stock (websearch for definition of that term), or anyone else. They owned a small percentage of the company; you bought it from them and gave them the money for it, just as you would buy anything else. You don't know or care who you bought from; they don't know or care who they sold to; the market just found a buyer and seller who could agree on the price.
There are a very few exceptions to that. The company may repurchase some of its own shares and/or sell them again, depending on its own financial needs and obligations. For example, my own employer has to purchase its own shares periodically so it has enough on hand to sell to employees at a slight discount through the Employee Stock Ownership Program. But you generally don't know that's who you're selling to; it happens like any other transaction.
And during the Initial Public Offering, if you're lucky/privileged enough to get in on the first wave of purchases, you're buying from the investment bank that's managing this process ... though that's an almost vanishingly rare case for "retail" investors like us; we're more likely to get the shares after someone has already pushed the price up a bit.
But really, when you buy a share the money goes to whoever you bought it from, and that's all you can know or need to know.
The money goes to the seller. There are a lot of behind the scenes things that happen, and some transactions are very complicated with many parties involved (evidenced by all the comments on @keshlam's perfectly reasonable high-level answer), but ultimately the money goes to the seller. Sometimes the seller is the company. The billions of shares that change hands each day are moving between other individuals like you and investment funds; these transactions have no direct impact on the company's financials, in general.