Who to ask: Ask teachers that can speak to your ability. The purpose of the letter is emphasize your academic strength, so ask those in whose classes you perform well. It is beneficial if the teachers you ask know you. If you sit in the back, never speak, and do the bare minimum, it will be hard for a teacher to write much (regardless of your grade). If you have an academic adviser, they are also a great candidate to write a letter for you, as they should know your interests and ambitions, as well as what drives you to succeed.
When to ask: ASK EARLY!!! Most teachers are very busy. It is important to ask them at least a month before your first deadline. It is courteous to provide each of your writers with an addressed and stamped envelope (unless it is an online recommendation). Inform them what the letter is for and when the due date is. It is ok to remind writers of upcoming due dates (just try not to go overboard). A recommender may alert you when the letter has been sent but it is a good habit to follow-up just in case. Remember to be polite and courteous during interactions with your recommender.
Format: There is no set format for letters of recommendation. If there is a standardized form to be used, make sure to have it available when you ask your recommender. Be sure to full out any required personal information on the form.
References: You may want to include the individuals that write you letters as references for employment or internship opportunities. This is perfectly normal, but just check to make sure they will be willing, and that you have up-to-date contact information.
Many companies will want a former boss or supervisor as a reference, someone who can speak to your work-ethic. If you work in a research lab or as an assistant to a professor, they would be great candidates, as they can speak to how you act in a work and educational environment.
Remember, getting great letters of recommendation requires preparation!
Check out the following sources for more information
This source has good first person information from a professor: http://matt.might.net/articles/how-to-recommendation-letter/
This source has good general information about letters of recommendation: https://owl.english.purdue.edu/owl/resource/970/01/
This website has some good advice on how to make the process easier for the faculty member: http://web.csulb.edu/~psy301/lettersrec.html