It's important to understand how memory is used. A significant portion of memory is often shared. Any code used in a program and it's libraries are shared between different processes. Code is mapped into memory as read-only and is shared by all processes so it not owned by any one user. Certain libraries like
libc are used by practically every single process, but are only loaded in RAM once. Programs that are more code heavily will have more shared memory than programs that are more data heavy.
To answer your question, nearly all the information that programs like top, free, and ps get comes from scanning the
/proc filesystem. Every process has it's own folder named after it's PID. In there are a number of files like maps that show's it's memory map, comm that shows the command that was executed to start the process, and io that shows I/O usage. There's an introduction to
/proc in the Linux Kernel Documentation.