Your first goal as an investor is to identify stock market opportunities. That's the point of investing, after all. If you can earn 15% to 20% annualized returns, you're happy.

But investing doesn't end there.

Taxes, fees, and trading costs add up over time. If you're not smart about your asset allocation, trading practices, withdrawal rates, and tax-advantaged vehicles, they will take a bite of your real returns. And the returns from those after-tax, after-trading costs are, after all, what matter most. They may not be the numbers you brag to your friends about, but they sure are the numbers that pay the bills.

Get ahead, stay ahead
Let's say you were a shareholder of one or more of the runaway market successes of the past year: Elan (NYSE:ELN), Garmin (NASDAQ:GRMN), and Qwest Communications (NYSE:Q) come to mind. (And if you held a position in any or all, congratulations are in order.)

Each of those stocks is up more than 100% since June 2005. And while you should be toasting to those returns, a few bad moves can make your super paper returns a little more average.

Now let's say you want to take some of your Elan off the table. If you'd bought in July of last year and sold today, you would have to pay capital gains at your personal income tax level (up to 35%!). If, however, you wait a month, you qualify for long-term gains and would likely pay only 15%. On a $5,000 investment, those 30 days could save you nearly $500.

Sure, that's a straightforward example, but let me assure you that the tax code is as complicated as it is long. If you've dollar-cost averaged into your positions, or if you want to withdraw funds from your Roth IRA, you've got a pile of math and legalese to wade through.

Profit from your losses
As we all know and have experienced, stocks don't always go up. The stock market is full of disasters. From the Great Depression to the tech bubble, investors in every era have experienced the pain of financial loss.

But the good news is that a few stock market duds don't signal the end of your financial future. In fact, a few losers can actually help your returns, because you can use stock market losses to write off stock market gains. Want to unload Garmin or Qwest? Maybe you also own XM Satellite Radio (NASDAQ:XMSR) or Dell (NASDAQ:DELL); these two stocks are down significantly over the past 12 months. It's only prudent to use your losses there to keep your gains out of the grubby hands of the tax man.

Or maybe you only own shares of XM and Dell. If so, then this has been a rough year so far. But don't fret: Even those who have been crippled by such losses can get started on a plan to rebuild their savings and investments and achieve their retirement dreams.

One of the first steps in such a plan is to make sure that your investments match your investing timeline. XM could be hugely successful in the future. Unfortunately, its current state of affairs isn't so rosy. It might be years before the company is consistently profitable and ready to wow analysts. If you don't have years -- read: you're in or very near retirement -- then XM is not the place for your money. Sell your shares to someone many years your junior.

If, however, you're a younger investor with some risk tolerance, consider taking a chance on a growth company such as Vertex Pharmaceuticals (NASDAQ:VRTX), which is not profitable right now, but can point to a stacked pipeline. There will be more bumps along the way with Vertex than if you invest in a more proven health-care stock such as Johnson & Johnson (NYSE:JNJ), but you've still got some years in you, so why not swing for the fences?

Listen to legends
The world's greatest investors can't predict the future. Jack Bogle, founder of the hugely successful Vanguard Group, told Fool co-founder David Gardner, "I am a nervous investor. Nobody knows what will happen tomorrow or next year. ... But [with] four things -- diversification, tax efficiency, lower costs, a long time horizon -- you will capture the returns that this uncertain stock market is kind enough to deliver."

Bogle's words should guide us all. While investing is the foundation of wealth building, there's always work to be done to perfect your portfolio, balance your investments, and allocate your resources among taxable and tax-advantaged accounts in a way that meets your timeline and protects you from financial stumbles. By being smart with your gains, you can benefit most from your stock-picking successes.

The goal of our Motley Fool Rule Your Retirement service is to help you do just that. Former Wall Street financial planner Robert Brokamp (or Fool Dad, as he's come to be known in these parts) will help make sure you don't get blindsided by arcane government regulations so that you earn the best real returns possible -- and plan for the perfect retirement. He'll even highlight specific stock picks from the Fool's best analysts along the way. To see exactly what Robert can offer free for 30 days, click here. There is no obligation to subscribe.

Tim Hanson does not own shares of any company mentioned in this article. XM Satellite Radio and Vertex Pharmaceuticals are Rule Breakers recommendations. Johnson & Johnson is an Income Investor recommendation. Dell is both an Inside Value and Stock Advisor pick. Garmin is a Stock Advisor pick. No Fool is too cool fordisclosure.