It's very simple consider if you have a user account in any website means your details are stored in that particular website server/host. Each and every time you access your account details by entering some details as username and password. This was a front end purpose only. On the backend, the process was different while you sign up on that website to make connecting between you and that particular website at that time a key(eg:adgot-1247) is generated in the backend and you can't able to see that key.
When you log in again with you log in metrics same key(eg:adgot-1274) will be generated for you every time and you can access your account.
"In your question, you ask how the items in a cart as a guest change while logged into that account after you logged in your backend key sent to the server and you access your particular saved account. Where you won't get any added cart items as a guest because it never saved with any of the particular accounts in a server it saved only for general purpose reference."
By this method, you can see a recommended product in some e-commerce websites while you using it with your own account. If you use your guest account you won't get any recommendation product according to your last viewed product at all...
I think this may solve your query if not feel free to ask in comment...!!