Let me get to know you by participating in class discussion and/or by visiting my office hours. Show genuine interest in, and insights about, the course material. Of course, this requires that you come to class prepared, having done the course readings in advance.
Too late to do this? I probably won't be able to write you a strong letter of recommendation, which in most cases, means that I shouldn't write you a letter at all.
It is best to do this in person, although sometimes (depending on the time of year, etc.) email is the only available route. If you are emailing me and it has been awhile since I had you in class, attaching a picture would be helpful.
Do not wait until the deadline is imminent; again, I can make exceptions, but keep in mind that professors have very busy schedules (hint: not just teaching) and I will have to juggle deadlines to fit in your letter of recommendation. It takes time to write a good letter of recommendation, especially if it's been awhile since I had you in class; it requires looking back on old assignments, email exchanges, etc., so that your letter contains specifics and not just platitudes about what a fantastic student you are.
I rarely refuse to write a letter; if I do, it's probably because I don't think I can write you a good one, so it is probably in your best interests not to insist.