Basically, you need to knit a swatch- cast on 25 or 30 stitches and knit a couple of inches. Then measure how many stitches you get in 2 inches of width, and how many rows you get in 2 inches of length (or 1 inch each way, or whatever). If you're using a pattern, knit your gauge swatch in that pattern. Say you get 10 stitches in 2 inches, and you want your afghan to be 30 inches wide- you'd cast on 150 stitches.
Mommy to two wonderful girls
"I don't have to conform to the vagaries of time and space, I'm a loony!" -Campbell Bain (David Tennant) 'Takin' Over the Asylum'
If you aren't using a pattern, you can knit a small piece and figure out your gauge, and then using that and how wide you want your afghan to be to figure out how many stitches to cast on. This is somewhat of a trial and error thing.
So say, for example, you cast on 20 stitches and knit for about four inches and your piece turns out to be 4 inches wide (you want to knit for at least 3 or 4 inches so you can get a fairly accurate measurement).
Say you want your afghan to be 48 stitches wide.
48 divided by the 4 inches wide of your trial piece is 12.
Multiply 12 by the 20 you cast on and you get 240 stitches to cast on.
Keep in mind that if you are doing stockinette stitch, it's a good idea to knit 4 or 5 stitches of garter on either side of your swatch where you are going to measure because stockinette curls and will make your measurement inaccurate.
I hope that makes sense!
If at first you don't succeed....rip it and try again!