You need to collect your main box outside the environment group:
\def\endcollecttikzpicture{%
\end{tikzpicture}\egroup
\aftergroup\collectbox
}
\def\collectbox{\setbox\mainbox=\hbox{\unhbox\mainbox\unhbox\tempbox}}
Note, that this makes the \mainbox contain the picture, it still has zero size because of the overlay option passed to the tikzpicture. If you remove that the box attains the natural dimensions of the square.
As you said you would prefer a vbox:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\makeatletter
\newbox\tempbox
\newbox\mainbox
\def\collecttikzpicture{\@testopt\@collecttikzpicture{}}
\def\@collecttikzpicture[#1]{%
\global\setbox\tempbox=\hbox\bgroup\begin{tikzpicture}[#1]%
}
\def\endcollecttikzpicture{%
\end{tikzpicture}\egroup
\aftergroup\collectbox
}
\def\collectbox{\setbox\mainbox=\vbox{\unvbox\mainbox\box\tempbox}}
\makeatother
\begin{collecttikzpicture}[remember picture]
\draw [cyan,fill=yellow]
(0cm,0cm)--(2cm,0cm)--(2cm,-2cm)--(0cm,-2cm)--cycle;
\end{collecttikzpicture}
\begin{collecttikzpicture}[remember picture]
\draw [cyan,fill=blue]
(0cm,0cm)--(2cm,0cm)--(2cm,-2cm)--(0cm,-2cm)--cycle;
\end{collecttikzpicture}
\begin{collecttikzpicture}[remember picture]
\draw [cyan,fill=green]
(0cm,0cm)--(2cm,0cm)--(2cm,-2cm)--(0cm,-2cm)--cycle;
\end{collecttikzpicture}
[[[\box\mainbox]]]
\end{document}
\endcollect...runs inside the group for yourcollecttikzpictureenvironment so as you made a local setting of\mainboxit is not set outside the group. Perhaps you just need\globalbut I'm note sure what teh code is intended to do. – David Carlisle Jun 12 '12 at 23:00