If a Finder window is opened while another Finder window exists, cmd+w will exit to the other Finder window, not previous app I am working on Example 1: Suppose I open up a Finder window, then I start to browse in Chrome. After a while I forgot there is already a Finder instance, and open up a new Finder window. Upon hitting ⌘+w to close this new instance, the system automatically takes me to the very first Finder window I opened, not the Chrome window which I am currently browsing.

Example 2: While in Finder, I click a pdf 1.pdf file and open it. Then I click a second pdf file, 2.pdf. Now if I use ⌘+w to close 2.pdf. The system will again automatically brings me to the window of 1.pdf, not the Finder window which I was previously working on.

Question: Is there a way to modify the behavior of ⌘+w? so that while I close a window from an app A, I am exited to the app B before I open A, not other older instances of app A.
No. To follow your first example, the Finder is the active application at the time you hit ⌘+w to close the second Finder window. So you remain in the Finder and the first Finder window is brought to the front.

One thing you could do is switch back to Chrome first (e.g. with ⌘+tab), then click the second Finder window's close button (assuming it isn't obscured by the Chrome window). That'll close the Finder window in the background while keeping Chrome as the active application.