Adaptive thresholding based on number of pixels
The book Digital Image Processing by Gonzalez and Woods provides a good introduction into standard techniques for automatic binarization. The Otsu technique is a popular one, and it's good practice to implement it on your own at least once.

http://www.imageprocessingplace.com/

But above all: give it a try! Look at a histogram of grayscale values in your image and then ask yourself whether the distribution of grayscale values immediately suggests a technique. Even if you're not sure how to work with the histogram, you might have an idea of the solution that you'd like to implement.

It's not easy to learn image processing just from online sources in bits and pieces. If you plan to work in image processing, and if you can afford it, try to have your own copy of at least one image processing book for reference. The Gonzalez and Woods textbook is a good standard reference, and is used in a number of undergraduate courses.

You can use the OpenCV library, buy the O'Reilly book, read forum posts and questions about OpenCV, etc., but in my opinion the Gonzalez and Woods book will ease you into the subject more gracefully.  

As always, if you are trying to solve a problem related to a specific image, please post the image. People who work in image processing for a living can often tell you a lot based just on an image; otherwise it can be difficult to provide more specific advice than just "read this chapter." A binarization technique that works well for one set of images can perform poorly for another set of images.