How to name binary (inorganic) compounds given their chemical formula, and vice-versa?
Prerequisites

These are the necessary prerequisites for this topic. If you're uncomfortable with any, please first head over to the corresponding links before continuing.


Chemical symbols are a shorthand method of representing an element. Instead of using the full name of an element, we simply refer to it with one or two letters. $\ce{N}$ for nitrogen and $\ce{Na}$ for sodium. More details on the Wikipedia page.
Polyatomic anions/Radicals: anions with more than one element, like nitrate ($\ce{NO3-}$) or sulfate ($\ce{SO4^2-}$). More details on the Wikipedia page.
Oxidation state: is an integer or decimal number assigned to an element in a chemical species. It is a tool that helps us do nomenclature easily. Read a detailed introduction here.
Ionic and covalent compounds: You must understand what ionic and covalent compounds are. You must also know the few elementary examples of each. For example, you should know that $\ce{N2O4}$ would be a covalent compound, while $\ce{NaCl}$ would be ionic. Here's an introduction by LibreTexts if you need a refresher.




Introduction

There are two separate cases here for ionic and covalent compounds.[1]. Some of the logic overlaps, but some does not, and care must be taken to avoid using one's rules for another.



Naming an ionic compound from its formula


Name the cation and then the anion. ("sodium sulfate", but not "sulfate sodium"). (How to identify the cations and anions in an ionic compound? The cation is usually a metal and the more electropositive element, while the anion is a non-metal and more electronegative element)
Keep everything lowercase (details) ("sodium sulfate", but not "Sodium Sulfate" or "sodium Sulfate" or "Sodium sulfate")
Do not apply numeric prefixes to either the cation or the anion. ("sodium sulfate", but not "disodium sulfate" or "sodium monosulfate")
Remember the suffixes for anions. The single atom anions of group 15, 16, and 17 have the suffix ending "-ide". Examples: fluoride, oxide and phosphide. However, polyatomic anions generally do not end in an -ide. See the Appendix for details.
Transition metal cations (and some p-block metals) also need their oxidation-state to be specified in parentheses, as they exhibit variable oxidation states.  Hence, "mercury(II) chloride", but not "mercury chloride". Notice that the oxidation state is denoted in capital Roman numerals.[2]


Let's take an example: $\ce{MgCl2}$. We can identify it's composed of magnesium and chlorine. Thus, the name would be magnesium chloride – in all lowercase and with the cation (metal) preceding the anion (non-metal). There is no need to denote the oxidation state as magnesium isn't a transition metal.

Examples:

$\ce{CsF}$, $\ce{NaNO3}$, $\ce{Al2(SO4)3}$, $\ce{FeCl2}$, $\ce{Pt3(PO4)4}$

Answers: (hover to peek!)


   caesium fluoride, sodium nitrate, aluminium sulfate, iron(II) chloride or ferrous chloride, platinum(IV) phosphate




Writing the formula for an ionic compound from its name


First write the cation and then the anion ($\ce{KBr}$ but not $\ce{BrK}$)
Use parentheses to denote multiple polyatomic anions ($\ce{Mg3(PO4)2}$ but not $\ce{Mg3PO42}$). Avoid them for a single polyatomic anion ($\ce{Na2SO4}$ but not $\ce{Na2(SO4)}$)
Use the trick of cross-transferring oxidation states between the ions to determine the count of each cation and anion involved. This trick works because the formula should denote an electrically neutral species. (you can verify this mathematically) So, to write the formula for aluminium sulfate:

Note that:


In case both oxidation states have a common factor, you should cancel them. Example: magnesium oxide:
.
drop the number 1. Hence, potassium sulfide is written as $\ce{K2S}$ but not $\ce{K2S1}$.



Examples:

potassium phosphate, calcium chloride, magnesium cyanide

Answers: (hover to peek!)


   $\ce{K3PO4, CaCl2, Mg(CN)2}$




Naming a covalent compound from its formula


Name the less electronegative element and then the more electronegative element ("carbon monoxide" but not "oxygen monocarbide")
Apply suffixes to anions in exactly the same way as we did for ionic compounds. ("sulfur hexafluoride" but not "sulfur fluoride") Also, keep the name all lowercase as before.
Use numerical prefixes.[3] Hence "dinitrogen tetroxide" for $\ce{N2O4}$ but not "nitrogen oxide". Here's the first ten suffixes: $$\begin{array}{|c|c|} \hline
\mathrm{prefix} &amp; \mathrm{number}\\ \hline
\mathrm{mon(o)} &amp; 1 \\
\mathrm{di} &amp; 2 \\
\mathrm{tri} &amp; 3 \\
\mathrm{tetr(a)} &amp; 4 \\
\mathrm{pent(a)} &amp; 5 \\
\mathrm{hex(a)} &amp; 6 \\
\mathrm{hept(a)} &amp; 7 \\
\mathrm{oct(a)} &amp; 8 \\
\mathrm{non(a)} &amp; 9 \\
\mathrm{dec(a)} &amp; 10 \\ 
\hline
\end{array}$$ Note that:


the letter in parentheses is dropped when the anion begins with a vowel ("tetroxide", but not "tetraoxide")
we do not specify the mono- prefix for the first element ("carbon tetrachloride" but not "monocarbon tetrachloride")
we can construct similar numerical prefixes for larger counts by using the Greek system. Example: "undeca" for 11. Look at a larger list on Wikipedia.



Let's take an example: $\ce{P4O6}$. In one molecule of $\ce{P4O6}$, we can see that it has four atoms of phosphorus and six atoms of oxygen. So, its name should be of the form "Xphosphorus Yoxide". The table of numerical prefixes tells us that we should use the prefix "tetra" for nitrogen and "hexa" for oxygen. Since the name "oxygen" begins with a vowel, we shall write "hexoxide" (and not "hexaoxide"). Thus, the correct name for this chemical formula is "tetraphosphorus hexoxide".

Examples:

$\ce{CO2}$, $\ce{P4O10}$, $\ce{N2O}$, $\ce{N2S5}$

Answers: (hover to peek!)


   carbon dioxide, tetraphosphorus decoxide, dinitrogen monoxide, dinitrogen pentasulfide




Writing the formula for a covalent compound from its name

This is much simpler from the previous case, rather it follows an exactly reverse process. 

We'll just take a look at two examples:


boron triiodide: we see that its formula would be: $\ce{B_$x$I_$y$}$. Now, $x=1$ since boron lacks any numerical prefix, and $y=3$ due to the "tri" prefix. Hence, the formula is $\ce{BI3}$.
dinitrogen pentoxide: we see that its formula would be: $\ce{N_$x$O_$y$}$. Now, $x=2$ and $y=5$ due to the "di" and "pent(a)" prefixes. Hence, the formula is $\ce{N2O5}$.


Examples:

hydrogen bromide, iodine heptafluoride, diboron hexahydride 

Answers: (hover to peek!)


   $\ce{HBr}$, $\ce{IF7}$, $\ce{B2H6}$




Naming of hydrogen compounds

Several hydrogen compounds have non-systematic names that have persisted through the years in standard nomenclature and are now well recognized. Note that for alkali metals and alkali earth metal hydrides, the name is simply "metal + hydride", like sodium hydride, on the basis of rules described earlier. 

However, covalent hydrides have varying names. There are not much rules around them, but rather "patterns" that the following list would show you:


Group 13: $\ce{BH3}$: borane, $\ce{AlH3}$: alumane, $\ce{GaH3}$: gallane, etc. Pattern: half of the element name suffixed by "-ane".
Group 14: $\ce{CH4}$: methane, $\ce{SiH4}$: silane, $\ce{GeH4}$: germane. Pattern: half of the element name suffixed by "-ane". Methane is a remnant of organic chemistry, and is a part of nomenclature of hydrocarbons.
Group 15: $\ce{NH3}$: ammonia, $\ce{PH3}$: phosphine, $\ce{AsH3}$: arsine, $\ce{SbH3}$: stibine, $\ce{BiH3}$: bismuthine. Pattern: half of the element name suffixed by "-ine". "Ammonia"'s origins, though, are a bit more interesting (read the Wikipedia page).
Group 16: $\ce{H2O}$: water, $\ce{H2S}$: hydrogen sulfide, $\ce{H2Se}$: hydrogen selenide, $\ce{H2Te}$: hydrogen telluride. Note that while "oxidane", "sulfane", "selane", etc. are acceptable, in the case of oxidane, that name is only used when denoting water derivatives. Read Do chemists refer to water as &quot;dihydrogen monoxide&quot;? and Does water have a chemical name?.
Group 17: they are simply named as "hydrogen halide". Example: "hydrogen bromide" $\ce{(HBr)}$. It is not named "hydrogen monobromide", as you may believe from earlier examples, and that it because hydrogen can only form a single halide with the halogens (see Note 3 below).


The curious students would also notice that – when writing chemical formulae – for groups 13, 14, 15, the hydrogen atom is written after the group atom $\ce{(NH3)}$, while in groups 16 and 17, it is written before the group atom $\ce{(H2O)}$. See this answer to understand why it is so.



Notes:


It is true that no compound is 100% ionic or 100% covalent. The above rules for "ionic compounds", thus, apply to compounds with dominant ionic character. Similarly, rules for "covalent compounds" apply to compounds with dominant covalent character.
Another way to write such a name is to use an "-ic" suffix for a higher oxidation state and "-ous" suffix for a lower oxidation state. As in "mercuric chloride" for "mercury (II) chloride". But, according to the IUPAC Recommendations 2005, it is no longer acceptable.
Why are numerical prefixes necessary? Note that in covalent compounds, a given anion can be present in more than one count. For example, the $\ce{F}$ in $\ce{BrF_x}$ can be present as $\ce{BrF}$, $\ce{BrF3}$, or even $\ce{BrF5}$. Hence, it is necessary to indicate if it is a monofluoride, a trifluoride, or a pentafluoride.
Several covalent compounds and ionic compounds have their own unique, common/trivial names. These names do not follow any systematic rules and are based on the source (location/discoverer) or properties of that compound. Learning these common names and their formulae comes by experience over time. Examples include: diborane $\ce{(B2H6)}$, Epsom salt $\ce{(MgSO4)}$, oil of vitriol (sulfuric acid), etc. Read more on the Wikipedia page.




Appendix: polyatomic anion names

Apart from the regular -ide suffix for monoatomic anions, there are some more suffixes for the polyatomic anions. 


most pseudohalides retain the "-ide" suffix. (except cyanate derivatives)
the other two suffixes are "-ate" and "-ite". "-ate" is used when the central atom is in a higher oxidation state, whereas "-ite" is used for the lower oxidation state.
the "per" prefix is used when the central  halogen atom is in its highest oxidation state, while "hypo" is used when it is in its lowest oxidation state. 


A few examples would make these rules clearer:


Sulfate $(\ce{SO4^2-})$ and sulfite $(\ce{SO3^2-})$: both are sulfur and oxygen anions, but in sulfate, sulfur has higher oxidation state as compared to that in sulfite. Similar case applies to nitrate, nitrite and phosphate, phosphite.
Hypochlorite $\ce{(ClO^-)}$, chlorite $\ce{(ClO2^-)}$, chlorate $\ce{(ClO3^-)}$, perchlorate $\ce{(ClO4^-)}$: have chlorine in increasing oxidation numbers. Similar series exist for other halogens (except fluorine).


Of course, there are many more polyatomic anions in chemistry, but these ions are relevant to our discussion at an elementary level.