You certainly don't need to know exactly how many pages your book is going to be before you even get started writing. But if you have a target, and you know what type of book you're creating, you can plan your budget accordingly.
The broad decision first: Do you want a hardback book or a trade paperback book? Hardback books are significantly more expensive to print, and because of the higher cover price, may sell less than a paperback book. But, for some books -- a mammoth textbook, say -- they're really the only way to go.
After you've made this decision, you can decide how many pages you'll want. Think about the scope of what you have to say and look at the page count in books with similar content. But also think about what you want the book to feel like. Simply pick out a book that is about the same size and format of what you have in mind.
When you find a good model to shoot for, count the number of words per page. Multiply that by the number of pages. Then subtract words for any "odd pages" -- the first and last pages of each chapter (these aren't usually filled), any blank numbered pages and any pages at the beginning and end of the book. This will give you a rough word count for the book. If you calculate how many words are on a page in your word processing program (or paper if you use a typewriter or if you write longhand), you can give yourself a target page count.
Why does this matter? For one thing, you need to think about the psychology of a book-buyer. If you're looking to create a gift book paperback, you don't want a massive 500-page volume, because it may feel too much like a reference encyclopedia. Its intended audience has more of a casual interest, so it should have a lighter feel. But if you're putting together a how-to guide, a 100-page book isn't going to seem like a good deal to your potential customer. They'll pick the thicker book on the shelf next to yours, because it seems more substantial.
Price also plays a role here. More pages costs more, and certain multiples of pages are cheaper than others. Printing presses print a set number of pages in one pass -- typically 32 pages, front and back. This means it's substantially cheaper to print a 320 page book than a 321 page book. This isn't something you have to figure out right away, but it should be a factor when you are laying out the finished book.