So here is the thing:
"how do you know you aren't a figment of your imagination?", you can reply all sorts of reasons, as to why this is impossible.
Now, Atheists say "people imagine God", cool.
The ontological argument is saying, we know we aren't imagining him, because he is the necessary being.
How do we attach "necessary" to God though and why can't I just attach to a pink unicorn and then pink unicorn exists?
It's due to the fact God is defined (when imagining per Atheists) to be the Greatest being. In terms of greatness, Anselm rightly pointed out, it's greater to be necessary then not to be and hence if you have non-existing God it's less great then the existing one (which is proven to exist by being necessary). Hence you know it exists.
Another way is to see it as a type of perfection as Descartes explained.
Or if you can think of possible worlds and existences, and know God's amount mathematically is big to the extent nothing is absent from it in terms of possible or existing, then it's proven by pigeon hole principle it exists and that's it the necessary being.
Another way to go about it, is to start with the premise existence is the default state. Either existence is necessary or just possible but not necessary. How do we know which one? To me, existence in it's purest form and self-existing form has none of nonexistence. This itself shows the Necessary being exists.
Then we can say how do we know this not energy, etc, well it's eternal by definition in this sense. It same form in all possible worlds. It includes all possible life and forms in it somehow, it is everything and yet nothing in particular.
And being necessary, would mean, there is no possible life that can existence independent of it, making it absolute in amount. This is God for sure.