What happens if you use a drone to build a building while he's carrying minerals?
Kind of a basic question, but what happens?
Do you lose it? Does it count as harvested?
In a similar fashion, what happens when a drone/scv/probe goes harvesting gas when he's holding a mineral?
It doesn't count as harvested until the drone deposits it at the nexus. The drone keeps carrying the mineral or gas until you send it to a nexus to deposit it.
Related to your second question, here's a good practice when switching a drone from minerals to gas: right click the assimilator, then immediately press "C" ("cargo"), which causes drones to deposit any minerals at the nexus before continuing to the assimilator. That way you can pick any three drones and send them to harvest gas: if they're carrying minerals, they'll immediately deposit them before going to the assimilator, and if they're not carrying minerals they'll ignore your "C" and go right to the assimilator.
(Credit for the best practice: @Emmett)