It's relatively dangerous, both ships have to be designed for it, and both crews have to be trained for it.
I participated in many such refueling and replenishing operations while I was in the US Navy.
https://www.google.com/search?tbm=isch&q=resupply%20at%20sea...
https://en.m.wikipedia.org/wiki/Underway_replenishment