It is implied that they are praying to Yahweh so that he will help them. See how you translated this in [Psalms 107:6](./006.md). Alternate translation: "Then they prayed to Yahweh to help them in their trouble" (See: [[rc://en/ta/man/translate/figs-explicit]])
Here David describes Yahweh saving them from their distress as if their distress were a physical place that he was bringing them out of. Alternate translation: "he saved" (See: [[rc://en/ta/man/translate/figs-metaphor]])