The most straightforward way of putting something in a framed box is the \fbox
command.
\documentclass{article}
\begin{document}
\newcommand{\sep}{\hspace*{.5em}}
\noindent
$\fbox{5} \sep \fbox{2} \sep \fbox{7} \sep \fbox{-5} \sep \fbox{16} \sep \fbox{12}$
\end{document}

Now that does not look very nice, the boxes are differently sized, depending on what is inside of them. To solve this we would need additional macros to determine the longest number in the input, etc.
It's probably better to use Tikz:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (s) at (0,0);
\foreach \num in {5,2,7,-5,16,12}{
\node[minimum size=6mm, draw, rectangle] at (s) {\num};
\coordinate (s) at ($(s) + (1,0)$);
}
\end{tikzpicture}
\end{document}

These boxes look a lot nicer, furthermore it also provides you with options for the shading and the brackets you have in your example. Have a look at the manual, Tikz can do pretty much anything.
For the sake of completion, I will add some code to get something close to the picture you gave as an example in the question.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcounter{nodeidx}
\setcounter{nodeidx}{1}
\newcommand{\nodes}[1]{%
\foreach \num in {#1}{
\node[minimum size=6mm, draw, rectangle] (\arabic{nodeidx}) at (\arabic{nodeidx},0) {\num};
\stepcounter{nodeidx}
}
}
\newcommand{\brckt}[4]{% from, to, lvl, text
\draw (#1.south west) ++($(-.1, -.1) + (-.1*#3, 0)$) -- ++($(0,-.1) + (0,-#3*1.25em)$) -- node [below] {#4} ($(#2.south east) + (.1,-.1) + (.1*#3, 0) + (0,-.1) + (0,-#3*1.25em)$) -- ++($(0,#3*1.25em) + (0,.1)$);%
}
\begin{document}
\begin{tikzpicture}
\pgftransparencygroup
\nodes{5,2,7,-5,16,12}
\endpgftransparencygroup
\pgfsetstrokeopacity{0.5}
\pgfsetfillopacity{0.5}
\pgftransparencygroup
\nodes{?,?,?,?,?,?}
\endpgftransparencygroup
\pgfsetstrokeopacity{.75}
\pgfsetfillopacity{.75}
\pgftransparencygroup
\brckt{1}{6}{0}{size=6}
\endpgftransparencygroup
\pgfsetfillopacity{0.5}
\pgfsetstrokeopacity{0}
\pgftransparencygroup
\brckt{7}{12}{0}{free space}
\endpgftransparencygroup
\pgfsetstrokeopacity{0.5}
\pgftransparencygroup
\brckt{1}{12}{1}{capacity=12}
\endpgftransparencygroup
\end{tikzpicture}
\end{document}
This provides a \nodes
command that takes a list of array values and creates boxes accordingly. The counter is used here to make labelling a little easier than with the updated coordinate. Note that you should reset the counter when you want to create a new array. The \brckt
command allows for a bracket to be drawn and takes as an argument the index from, the index to, the level and the text to be put on the bracket. The level is used to determine how far from the nodes the bracket should be drawn. The use of the transparency groups is due to some strange issues I got, couldn't figure out what was happening exactly, so just wrapped everything up in a group to make it work. If some one knows why the groups are needed, please add it and let me know?
The final result looks like this:

Fixing the spacing and bracket placement to match the example exactly is left as a (not too difficult) exercise to the reader.
\underbracket
.