The most important step is to compare the gauge. The yarn suggested in the pattern and the yarn you are thinking to use should give you the same gauge when knitted up. This way you make sure the sizing will be the same. Most yarn bands will give you an estimate of how many stitches and rows the yarn works up to on a certain size needle over an inch or four inches. This is often shown graphically with a number of rows and stitches on a grid with a particular needle size named outside the grid.
To find yarn which has the same gauge you will look at yarns of the same weight (fingering, sock, DK, bulky etc).
Another point to consider is the fiber of the yarn. Go with yarn you like to work with or able to wear (if you are allergic to wool choose a cotton or linen etc). There are so many yarns you can choose from that it is worthwhile looking till you find the one you really like.
Now you will need to consider how much of the yarn to buy. In the pattern you should find the number of skeins or hanks used to complete the project you should also find the yardage of each skein. This will help you to calculate the total yardage. Make sure you buy that amount of yardage. Of course most patterns don’t use up every last scrap of yarn and you may have plenty of yarn left once your project is completed depending on the yardage in each skein of your substitute yarn. I recommend not winding up all your skeins at once. Most yarn stores will let you bring back unused full skeins in their original form.
Before you start on your project make sure you knit up a gauge swatch. This is especially important when you substitute yarn. It seems like a lot of work when what you really want to do is start your project, but just because the yarn band says a yarn will work to a certain number of stitches doesn't mean it will when you are holding the needles.
Every knitter is different, and even small variations in the number of stitches per inch can make a big difference in the sizing of a knitted article. When you learn that you need to use a different sized needle to get the desired result based on doing the swatch you will be glad that you knitted up the swatch. It's much better than knitting your whole project and then finding it doesn't fit.