What happens if you use a drone to build a building while he's carrying minerals?
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)
The only time you collect the minerals is if the worker brings it back to an existing base building (Hatchery, Nexus, or Command Center). Under any other circumstances the minerals or gas are lost.