The stock market frequently moves both up and down, and individual stock prices can be very volatile, so just because something is bought or sold at a certain price does not necessarily mean that is its actual value. A rational investor would expect their investment to increase in value or at least expect to be paid back something of equal value. Basically, what this means is that you could pay a low price (it is cheap) for something with a high value, but also pay too much (it is expensive) for something with a lower value.
Theoretically, the actual value may only be determined by each person (see subjective theory of value), where some investor could value something a lot higher than another investor, especially when it would provide something it would not necessary provide someone else (such as voting rights, or a non-stock related value could be productivity or similar services). However, and in the context of stocks and the stock market, it is somewhat safe to assume that most participant want their investment to increase in value.
Assuming most investors want to increase the value of their investment, such as buying undervalued stocks and selling overvalued stocks, the overall stock market should be fairly efficient at determining value (see efficient-market hypothesis), so the current market cap of most companies should be close to its actual value (as determined by the thousands of stock market participants). However, fairly efficient is good enough to find opportunities, especially in individual stocks, foreign or smaller markets, or anywhere where there might exist some information asymmetry (these are easier to find among less known companies).
Determining the value of a company (or share price) is a problem faced by most investors and it could be described as a process rather than a method, which means that it is hard (if not impossible) to automate. This is due to most investors having their own unique personality, prior experience, and different goals. One approach is to consider the act to buy stock in some company as buying the whole company, where the investor (or soon to be business owner) own all of the outstanding shares. In this particular case, the value could be whatever the investor paid for the shares, which should be close to the value of all the assets, but more realistically, the value would likely be a combination of its assets and estimated future earnings (as adjusted to inflation and risk).
One simple method to estimate future earnings is to represent earnings as a function of revenue and cost, where various profitability margins often get better over time (but not always). For most comapnies, it is generally safe to assume slowing growth over time, but improved margins and earnings (which often is the sole purpose of most CEOs).
The income statement contains all figures needed to estimate earnings, where revenue is the key metric, but can sometimes itself be a function of other metrics, such as revenue segments, or products sold. It is also important to keep an eye on potential non-cash expenses, which can be confusing (see stock-based compensation), but it is common to exclude if significantly larger than other expenses (otherwise the impact on earnings could be misleading). Ratios and tax can be used to make sure that the estimation is realistic, such as gross margin, operating margin, and tax rate, as well as determining year-on-year change in revenue, or various operating expenses (ratios should not change too much, unless there is some explanation). Over time, gross margin should decrease due to competition and operating margin should increase due to maturity (more efficient, reduced spending).
The balance sheet is useful to make sure that the company have enough cash to cover any short-term liabilities (or any liabilities at all), and in particular enough cash for near-term debt repayments. The cash flow statement is used to make sure that the company is financially stable, where cash generated by the company should approximate the reported earnings over time (see Free cash flow). Note that cash flow (as found on the cash flow statement) also can be used to estimate future earnings, since generated cash is earnings in disguise.
Microsoft Excel is the only tool needed to build a financial model (it is also the most used AFAIK), where each quarter or annual report is typed into columns (one column per quarter, similar to the layout in earning reports), input several years of previous earnings before attempting to estimate future earnings. This process takes time, and any estimation should be more accurate with specific knowledge in whatever industry the company is operating in, where investors need to consider likelihood of various events, sales targets, new products, and so on. A good model should estimate revenue and expenses for at least a couple of years into the future, then assume zero or negative growth, every year, until earnings are close to zero (this could be many years into the future).
The net present value (or NPV) is the sum of the discounted cash flow (this is the estimated future earnings). The discount rate should be in the 0-10% range and represents the perceived risk (as compared with alternative investments, including not investing at all). An appropriate discount rate should be the risk-free rate (time preference) plus some risk premium, and should reflect the likelihood that estimated future earnings does not happen, minimum expected return, or relative risk compared with peers. The discount rate should be higher than interest paid on bonds or other debt in the company, which typically ranks higher than common stock, and so lower risk. The NPV is then added to net cash, which is cash and marketable securities less debt (as found on the balance sheet). This is what you should pay for the whole company, or divide by shares outstanding to get price per share, which can be both higher or lower than the current market price.
Important: financial models should be seen as a tool to make decisions and not as proof of value, so tweaking and testing various scenarios (such as lower growth, gaining users, losing users, tax fine, and so on) should provide very valuable information about potential value, rather than actual value.
Buying and selling is not as complicated (technically, but perhaps harder mentally). In general, you should buy when your financial model seems accurate, risk is tolerable, and current market price is lower than the estimated price per share, or lower than estimated price per share with some margin of safety, and sell, or short sell, when current market price is higher than the estimated price per share. Another approach is to buy shares in companies with the most certain model and with highest potential return among many securities in an industry, subset of industry, or even more specific set (see universe of securities), and sell the least certain, or most overvalued as compared to rest in set. A set of securities, or universe, should be as big as there is enough time to properly cover each company in the set, and should include competition and peers (that would otherwise not be target for investment).