Example 7 Let $S=\left\{A=\left(a_{1}, a_{2}, \cdots, a_{8}\right) \mid a_{i}=0\right.$ or $\left.1, i=1,2, \cdots, 8\right\}$, for any two elements $A=\left(a_{1}, a_{2}, \cdots, a_{8}\right)$ and $B=\left(b_{1}, b_{2}, \cdots, b_{8}\right)$ in $S$, denote $d(A, B)=\sum_{i=1}^{8}\left|a_{i}-b_{i}\right|$, and call it the distance between $A$ and $B$. How many elements can be selected from $S$ at most, such that the distance between any two of them is $\geqslant$ 5?
(1995 Chinese National Team Selection Test)
Let $S^{\prime}$ be a subset of $S$, and any two elements in $S^{\prime}$ have a distance $\geqslant 5$.
Let $A=\left(a_{1}, a_{2}, \cdots, a_{8}\right)$ and $B=\left(b_{1}, b_{2}, \cdots, b_{8}\right)$ be two elements in $S^{\prime}$, and let $w(A)=\sum_{i=1}^{8} a_{i}, w(B)=\sum_{i=1}^{8} b_{i}$.
Clearly, $w(A), w(B)$ represent the number of 1s in $A, B$ respectively.
If $w(A)+w(B) \geqslant 12$, then $A$ and $B$ must have at least 4 positions with 1s in common, thus $d(A, B) \leqslant 8-4=4$,
which contradicts the definition of $S^{\prime}$. Therefore, it must be that $w(A)+w(B) \leqslant 11$.
From the above, in the elements of $S^{\prime}$, there can be at most one element with the number of 1s $\geqslant 6$.
Without loss of generality, assume $(0,0,0,0,0,0,0,0) \in S^{\prime}$ (since we can change the $i$-th component of every element in $S^{\prime}$ from 1 to 0 or from 0 to 1 without changing the properties of $S^{\prime}$).

Thus, the other elements in $S^{\prime}$ have at least 5 1s. If $S^{\prime}$ has two elements with exactly 5 1s, then by $5+5-8=2$, these two elements must have at least two positions with 1s in common. Since the distance between these two elements is $\geqslant 5$, they can have at most two positions with 1s in common.

In summary, any two elements in $S^{\prime}$ with exactly 5 1s must have exactly two positions with 1s in common, so $S^{\prime}$ can have at most two elements with exactly 5 1s.

From the above discussion, $S^{\prime}$ can have at most 4 elements: one with all 0s, one with the number of 1s $\geqslant 6$, and two with exactly 5 1s.
On the other hand, $S^{\prime}$ can be composed of the following 4 elements:
$(0,0,0,0,0,0,0,0),(1,1,1,0,0,1,1,1)$,
$(1,1,1,1,1,0,0,0),(0,0,0,1,1,1,1,1)$.
Therefore, in $S$, the maximum number of elements that can be selected such that any two of them have a distance $\geqslant 5$ is 4.