In a market economy, you can think of the FX operations of the Central Bank as a process of ebb and flow, or as a buffer function. During favorable times when the local currency is healthy and in good demand worldwide the CB sells local currency to buy USD (or other reserve assets) for its balance sheet; vice versa during periods of weakness they sell USD to buy the local currency. All at market prices.
In an economy with a non-convertible curency (controlled foreign exchange), the local exporters are required by law to sell all (or a portion) of the foreign exchange they earn from exports at a specified rate to the CB (or a sepcialized agency such as the State Administration of Foreign Exchange). So "all of your FX belong to us", and is managed by the CB as it sees fit (keeping it invested, selling it to importers for their use or using it for FX intervention).
Another possibility as you mention is to sell gold.