How to typeset piecewise-defined functions?

Option 1 (recommended)

left aligned

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage{mathtools}
\begin{document}
\abovedisplayskip=0pt\relax
\[
\lvert x \rvert =
\begin{cases}
x & \text{if } x>0\\
0 & \text{if } x=0\\
-x& \text{if } x<0
\end{cases}
\]
\end{document}

enter image description here

Option 2 (just for fun)

right aligned

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage{mathtools}
\begin{document}
\abovedisplayskip=0pt\relax
\[
\lvert x \rvert =
\left\{
\!
\begin{aligned}
x & \text{ if } x>0\\
0 & \text{ if } x=0\\
-x& \text{ if } x<0
\end{aligned}
\right.
\]
\end{document}

enter image description here

Important Notes

standalone class and \abovedisplayskip=0pt are settings for my own purpose to create a standalone and tight output. You should not use it for your production unless your objective is the same as mine.