God can hear all prayers because He is omnipresent and omniscient. Omnipresent means He is everywhere, so He can hear you. Omniscient means He knows everything, so He even knows your thoughts before you pray.
It’s difficult, if not impossible from a human point of view, to imagine how God can be everywhere and know all things. But it’s also difficult to understand how God could create the universe and all that is in it. Yet we know He did. Both His Word and what we observe all around us testify to HIs creative works. So also, God assures us in His Word that He hears our prayers. (John 5:15, 1 Pet 3:12).