The first time you know for sure the cost of a project is after the project is completed. Of course, you can't wait for the project to be completed to provide some sense for the resources that are required--that's something you need to do upfront.
Your estimating process will be much smoother and the resulting estimate more accurate if you prepare first. Before you begin the estimation, consider the following areas of preparation.
Get a clear picture of the work that is being estimated.
Many (perhaps most) of the problems that you have with estimating result from not being really sure what the work entails. You should avoid estimating work that you don't understand. If you're getting ready to start a project, you should know enough so that you can estimate the work to within plus or minus 10%. If you can't estimate the work to this level of confidence, you should spend more time investigating and understanding the work. If the work is just too large to be able to estimate at that level of confidence, consider breaking the project into smaller pieces so that you can estimate each smaller project to within 10%.
Determine who should be involved in the estimating process.
The project manager may or may not know enough to make the estimates on his own. It is usually a good practice to look for estimating help from team members, clients, subject matter experts, etc. For instance, there may be experts in your company that can provide more insight into the level of effort required to complete certain work. This will usually result in the estimates being far more accurate than you would get by yourself.
Determine if there are any estimating constraints.
If there are estimating constraints, it's important to know them upfront. For instance, if the end date is fixed (timeboxed) by some business constraint, make sure you know this going in. Likewise, if your client expects six-sigma quality in the deliverables, you will need to estimate higher. If you have a fixed budget you can make sure that the scope of the project can be achieved within the budget. Knowing all of your constraints will help you make valid tradeoffs regarding the cost, duration and quality balance.
Utilize multiple estimating techniques if possible.
There are many techniques that can be used when you get ready to do an estimate. These techniques include analogy, expert opinion, modeling, etc. Where possible, try to use two or more techniques for the estimates. If the estimates from multiple techniques are close, you'll have more confidence in your numbers. If the estimates are far apart, you should spend more time rationalizing the numbers to get them consistent. For instance, if you estimate the work at a detailed activity level, but your estimate is not consistent with an expert opinion, that should tell you to spend more time reconciling the two approaches until you achieve more consistency.
When you are asked to estimate a piece of work, resist the urge to jump right in. If you spend just a little time planning, you will find that the entire estimating process will be quicker and will result in a more accurate final estimate.