Knowing first what you can get for your trade ahead of time is mandatory. Then when trading it in don't expect to get every penny of that worth, the dealer does have to resell it at a profit. I'm positive there is a percentage they mark it up. You at that point can take the offer or walk. If that bothers you then selling it on your own is the best option as your in control of your profit (or loss).
You are also in control of the buying process as well. Knowing what your buying and going prices in the area is also mandatory. You must always understand the dealer is going to make a profit in either scenario. It all depends on how good you are at haggling getting your best deal. The internet is your friend researching all this information, and you can use it to your advantage. Not knowing anything about what your purchasing or selling will put you at the disadvantage.
We recently purchased a new car. I didn't step foot into a dealer until I walked in and signed the papers. All my negotiation was over the phone between 5 dealers. They all knew they were being quoted against each other. I knew what I wanted and I knew what they had on the lot and what others were getting on deals. I got a couple good offers and decided between the two. One dealer refused to negotiate over the phone and wanted me to come deal with him directly. I told him the other 4 didn't have a issue and I'm sure they would like be to "stop by" and deal directly.
It took me about 1/2 hour signing the papers and driving off. The dealer made money and I got a deal I was happy without spending hours going back & forth at each dealer.
Negotiate the trade and purchase separately. Lots of times they'll give you every penny on your trade just to roll the difference on what they want into the new purchase.
Expect them to make a fair profit.