If you are following a pattern, the pattern will tell you the gauge and how many stitches you should get per inch using a certain needle size. You grab those needles and the recommended yarn (or one the same ply etc) and knit a few rows and see how many stitches you are getting per inch. If you get the same as the pattern asks for then you have the right needles. If you are getting too many you go up a size and try again. If you get too few stitches you go down a size and try again.
Also, CO more sts than the gauge pattern says. If it's 20 sts for 4", use 24-30 sts and measure 4" in the middle. The edge sts are often a different size than those in the center so if you only use 20sts, you could be off and not realize it.