Read the file as written in the previous run.
\input plipsum % for mock text
% save the total page number in a file (hook into \bye)
\outer\def\bye{\par\vfill\supereject\writetotal\end}
\def\writetotal{%
\immediate\openout\totalfile=\jobname.tot
\immediate\write\totalfile{\the\pageno}%
\immediate\closeout\totalfile
}
\newwrite\totalfile
\newread\totalread
% at the start we read the file
\openin\totalread=\jobname.tot
\ifeof\totalread
% the file doesn't exist
\def\totalpage{0}%
\else
% define \totalpage to the contents of the first line in the tot file
\read\totalread to \totalpage
% it will have a space at the end
\fi
\closein\totalread
\footline{\hfil Page number \folio\ out of total \totalpage\unskip\hfil}
\lipsum{1-40}
\bye
If you insist to have a count register, just set it:
\input plipsum
\outer\def\bye{\par\vfill\supereject\writetotal\end}
\def\writetotal{%
\immediate\openout\totalfile=\jobname.tot
\immediate\write\totalfile{\the\pageno}%
\immediate\closeout\totalfile
}
\newwrite\totalfile
\newread\totalread
\newcount\totpages
\openin\totalread=\jobname.tot
\ifeof\totalread
\def\totalpage{0}%
\else
\read\totalread to \totalpage
\fi
\totpages=\totalpage
\closein\totalread
\footline{\hfil Page number \folio\ out of total \number\totpages\hfil}
\lipsum{1-40}
\bye