Yarns are classified into different weights. At the fine end are lace and fingering, DK and worsted are in the middle, with Aran, Chunky and Bulky at the thicker end. (This is a simplification, but man, there are a lot of classes of yarn.) It wasn't until I started consulting at Espace Tricot that I saw that what these classifications are, really, are gauges. These categories tell you what gauge this yarn is suited for. Most yarns carry this information on the label. It shows you a little square - 10cm x 10cm - and tells you how many stitches you can expect this yarn to make in that space, when matched with the appropriate needle.
Obviously, you can knit any yarn with any needle you like (although really thick yarn with really small needles may prove physically impossible). You can knit your fingering weight yarn (which lists 32 sts on 2 mm needles) on 5mm needles and get an open, airy fabric. Some patterns may ask you to do exactly this. But most of the time, the gauge listed in a pattern and the gauge listed on a yarn ball band are going to square up. Any minor inconsistencies are probably down to personal gauge - the designer's and yours - and can be accounted for by adjusting needle size.
Writing this down, it seems so obvious, but I honestly didn't really get it until I started helping other knitters find substitute yarns while working at Espace Tricot. My experience before then had been so personal, so much just doing my own thing, that I didn't see the bigger picture. Gauge is not the only factor in finding a substitute yarn – fiber content and texture are important too – but it sure does help. If you can go into your LYS and say "I need a DK weight yarn that knits up to 22 stitches per inch on 3.5mm needles in stockinette, with a smooth texture, and I tend to knit loosely" you will not only delight the clerk working there, you are much more likely to find suitable yarns.
Sometimes I see a knitting pattern in my Ravelry feed or in a book and think "Ah, I love that! But instead of buying new yarn to make it, I'm going to use something from my stash." But a visit to the stash later, I realize I don't have enough Worsted on hand, but I do have plenty of this lovely hand-dyed DK. And then madness of recalculating the pattern begins. For some patterns, it's easy to change yarn weights - a delicate shawl can be a thick wrap. A simple raglan sweater might take some number crunching, but hey, I've designed patterns from scratch, I can handle it!