I believe that most alkaloids are found as a salt within the plant. They are probably biosynthesized as the free base at first, but fairly quickly come into contact with an acid within the plant, or CO2 in the air to form the carbonate salt.
And yes, generally in production/extraction of cocaine they do the extraction from the coca in basic conditions, turning the salt form within the plant into the freebase. From there it is purified (look up acid/base reaction) and turned into it's salt form again. It doesn't have to go in that order though. They could also start by extracting it in acidic conditions in which case they would be starting with a salt, freebase it, and turn it back into a salt again.