First we use the geometry to find out what is really going on. Then we will be ready to go to the $M$ and $l$ stuff. Very informally, if $\lim_{x\to \pi/2} \frac{1}{\cos x} =\infty$, that would mean that if $x$ is close but not equal to $\frac{\pi}{2}$ then $\frac{1}{\cos x}$ is big positive.
If $x$ is a tiny bit below $\frac{\pi}{2}$, then $\frac{1}{\cos x}$ is indeed big positive, since $\cos x$ is positive and close to $0$. But if $x$ is a bit bigger than $\frac{\pi}{2}$, then $\cos x$ is close to $0$ but negative, so $\frac{1}{\cos x}$ is certainly not big positive. So the bad guys are the $x$ that are a bit bigger than $\frac{\pi}{2}$.
Now that we know what's going on, let's write a formal proof. I will try to use the symbols that you used.
What would it mean for the limit to be $\infty$? To save typing, and for the sake of generality, I will write for a while $a$ for $\frac{\pi}{2}$ and $f(x)$ for $\frac{1}{\cos x}$.
We have $\lim_{x\to a}f(x)=\infty$ if for any $M$, there is an $l>0$ such that for any $x$ such that $|x-a|, then $f(x)>M$. You can think of it this way. Suppose we are given a specific big $M$, like $1000$, or $99999999$. We want to be able to exhibit a number $l$ such that if the distance $|x-a|$ of $x$ from $a$ is $, then $f(x)$ is guaranteed to be $>M$.
Think of the number $M$ as a challenge, and of $l$ as a response. To the challenge "ensure that f(x)>M" we must always be able to come up with an appropriate response "if $|x-a|, then I can guarantee that $f(x)>M$." (The appropriate response $l$ will in general depend on $M$.)
The rest is easy. Let $M=17$. Can we come up with an $l$ such that if $\left|x-\frac{\pi}{2}\right|, then $\frac{1}{\cos x}>17$? Definitely not. For whatever $l$ we pick, there will be a number $x$ such that $\frac{\pi}{2} and $\left|x-\frac{\pi}{2}\right|. And at any such number $x$, the number $\frac{1}{\cos x}$ will be negative, so definitely not bigger than $17$. So for the challenge $M=17$, there is no possible response $l$.
We conclude that it is not true that $\displaystyle\lim_{x\to \pi/2} \frac{1}{\cos x} =\infty$. Indeed, a small variant of the argument shows that the limit in this case does not exist.
Comment: You wrote $\displaystyle\lim_{x\to \pi/2} \frac{1}{\cos x} \ne \infty$. I prefer not to do that, for writing it that way may carry the impression that the limit exists, but happens to be something other than $\infty$.
With some practice, this $M$, $l$ business, and related ideas, will become clearer to you. It is a genuinely subtle idea, and takes time to become fully absorbed.