For step-by-step tutorial: https://www.cubatravelservices.com/2017/02/18/fill-cuba-visa-card/
We recommend purchasing your visa prior to your travel date in order to ensure you have all required travel documents and to avoid any delays. You may purchase your visa at our Cuba Travel Services kiosk on the day of your flight depending on departing city. Check with your airline to see if visas are available for purchase on the day of your flight. Price and availability may vary.
Everyone traveling from the US to Cuba, Including International citizens residing in the US/ flying out of the US, must self qualify under one of the 12 licensed categories of authorized travel and have a Visa. You can familiarize yourself with the categories here: https://www.cubatravelservices.com/plan-your-trip/who-can-go/
Once you have familiarized yourself with the categories, you can visit https://www.treasury.gov/resource-center/sanctions/Pages/licensing.aspx in order to register and apply for “Travel to Cuba”.
After receiving approval to travel under one of the categories, you may purchase your Visa here: https://cubavisaservices.com/product/touristvisa-card/
Visa is only valid for 30 days. Hold on to the second half-of your visa (Cuban customs will keep the other part) in case you are asked for it later (although it rarely happens).