As the wallet can generate many public addresses from a private address. How does the wallet know that any transaction from or to the wallet is for that private wallet if the in and out can be any number of public addresses.
While exact implementations between wallets might differ, the general approach is usually similar to the following:
- A wallet generates a private key (or a set of keys) based on an HD wallet, or just individual random keys
- The public keys for private keys generated in 1. are calculated, and used to create the desired address scripts (p2pkh, p2sh (wrapped segwit/multisig), p2wpkh, p2wsh).
- The wallet stores the private key, along with a mapping to which address script uses that key.
- The wallet constantly scans the mempool and new blocks for outputs that send Bitcoin to the address scripts calculated in 2. When it discovers such an output, it adds the coins to the wallet balance and informs the user.
There are implementation specific variations on this (for example, an HD wallet may not store individual private keys, and store a mapping of the derivation path from the root key and the address script instead).
For an actual example, let's say our wallet has the private key for the address
1F1tAaz5x1HUXrCNLbtMDqcw6o5GNn4xqX. The HASH160 for the public key of this address is
99bc78ba577a95a11f1a344d4d2ae55f2f857b98, and the locking script is
76a91499bc78ba577a95a11f1a344d4d2ae55f2f857b9888ac. This locking script is what appears in the actual bitcoin transaction that sends coins to this address.
The wallet will store the private key, and make a note that the private key corresponds to that locking script. It will then scan the bitcoin blockchain for transactions which have an output that matches that locking script.
As the wallet can generate many public addresses from a private address.
I'm assuming by 'private address', you actually mean 'private key'.
Each private key effectively maps to one Bitcoin address, so for each new address your wallet creates, it will also first create a private key that the address is derived from. If your wallet is a hierarchical deterministic wallet, then it will have a 'master seed', but this seed is then used to derive subsequent private keys (and their corresponding public keys and addresses).
A wallet can watch the network for transactions that create UTXOs which are spendable by a key the wallet controls. How exactly this is accomplished can vary depending on the wallet architecture (full-node vs light wallet, etc).