In memory paging, how does the S.O. know where the page is in secondary Memory?
The first thing is that when the page-table entry is invalid you are allowed to use all the other bits in the page-table entry for whatever you want.  So, for example, you could use it to store an offset into the swap space, or a disk block number.

But that's actually not the whole story.  Each process on most operating systems has some kind of segment map that describes for each valid range of logical addresses what's in that range.  Ranges that contain read-only data (such as the code segment, usually) don't need to be swapped.  Instead they can just be reread from the executable that contains them if they are needed again.