This excellent video from the lovely Tyler de Witt explains how to write the formula of an ionic compound: