How to understand "你怎么了？"
It's one of those fixed expressions whose otherwise regular meaning is significantly and conspicuously altered by the modal 了, that introduces change semantics.
The phrase 「你怎么（样）～」 in itself means &quot;How do you...?&quot;. If you add a modal 了 signifying change, it becomes：

&quot;How do you... now&quot; (as opposed to before)

...which in an idiomatic translation becomes: &quot;What happened to you?&quot;. The English version precisely conveys change semantics, as it implies that you had no issue earlier, but you might have some now.
A similar result happens with this other expression:

我知道 I know
我知道了 I know now = I got it

And also this one, though I must admit this is my own interpretation, and I have no scientific evidence to support it:

算 to count, to consider as
算了 counted (as in, not counted before, but counted now) = forget about it