How does Proof-of-Stake works programmatically I've researched on the internet, but am still confused about how Proof-of-stack works programmatically.

As mentioned here, in Proof-of-Stake system, A random Nextcoin is selected to be the next "miner". Which node is designated to randomly select a node as the next "miner"? Or in other words, how can a system agree on a random number in a way that can't be manipulated?
In most proof of stake systems, there isn't really some node that says "this node is the next staker". Rather what many do is, using data from the blockchain, calculate a set of public keys (the set can just be one public key) that can be the next staker. If a node that has the private key for one of those public keys is online, they can construct the next block.

The way that the set of public keys is determined varies by coin, but they all use data from the blockchain and a deterministic algorithm so that all nodes will come to the same set of public keys.

The randomness in these algorithms typically comes from hashing data which could not have been known beforehand (e.g. for block n, take the hash of block n-1). In this way, a random number is generated such that all nodes on the network will generate the same number.