Yes, you can. This is how.
In Diamond/Pearl you talk to either of the ladies near the door and connect to WI-FI. If your friend is logged on, it will indicate as much. You can hit 'invite' and then 'trade' to trade. Your wanting to trade will be represented on your friend's secreen. If they accept, you can veiw their Pokemon and they can veiw yours and decide what to trade. You can only trade Pokemon in your party. Once you and your friend agree on pokemon, you trade.
In HG/SS you get into talk to the lady in normal clothes, access WI-FI the same way and then go up to the computer and hit 'trade'. On your friend's screen, you will start bouncing. If they hit you, it will say you want to trade. If they agree to trade with you, you can veiw their Pokemon and they can veiw yours and decide what to trade. You can only trade Pokemon in your party. Once you and your friend agree on a pokemon, you trade.