It takes time and there are other things that will pay off far more in the long run. This is a gross simplification of the whole process. With that said, let's dive in. This discussion will focus on spider engines. That is, an engine that goes to your site and indexes you based on what it finds.
Every search engine is different.
An algorithm is a set of rules. These algorithms change constantly. The only reliable way to learn a sites algorithm is to analyze actual results of a search on that engine. This must be done using a reliable keyword density analyzer which will show you the weight of particular keywords in high ranking documents. You then simply reproduce this weight in your document to attempt to reproduce the results.