  By a famous result of Doyen, Hubaut and Vandensavel \cite{DHV}, the 2-rank of
a Steiner triple system on $2^n-1$ points is at least $2^n -1 -n$, and equality
holds only for the classical point-line design in the projective geometry
$PG(n-1,2)$. It follows from results of Assmus \cite{A} that, given any integer
$t$ with $1 \leq t \leq n-1$, there is a code $C_{n,t}$ containing
representatives of all isomorphism classes of STS$(2^n-1)$ with 2-rank at most
$2^n -1 -n + t$. Using a mixture of coding theoretic, geometric, design
theoretic and combinatorial arguments, we prove a general formula for the
number of distinct STS$(2^n-1)$ with 2-rank at most $2^n -1 -n + t$ contained
in this code. This generalizes the only previously known cases, $t=1$, proved
by Tonchev \cite{T01} in 2001, $t=2$, proved by V. Zinoviev and D. Zinoviev
\cite{ZZ12} in 2012, and $t=3$ (V. Zinoviev and D. Zinoviev \cite{ZZ13},
\cite{ZZ13a} (2013), D. Zinoviev \cite{Z16} (2016)), while also unifying and
simplifying the proofs. This enumeration result allows us to prove lower and
upper bounds for the number of isomorphism classes of STS$(2^n-1)$ with 2-rank
exactly (or at most) $2^n -1 -n + t$. Finally, using our recent systematic
study of the ternary block codes of Steiner triple systems \cite{JT}, we obtain
analogous results for the ternary case, that is, for STS$(3^n)$ with 3-rank at
most (or exactly) $3^n -1 -n + t$. We note that this work provides the first
two infinite families of 2-designs for which one has non-trivial lower and
upper bounds for the number of non-isomorphic examples with a prescribed
$p$-rank in almost the entire range of possible ranks.
