Why do I sometimes get an attachment id string instead of a url from an image’s guid field in the post table?

GUIDs aren’t URLs, they are unique identifiers for feed readers. The GUID should never be used in any other way.

See wp_get_attachment_url and get_attachment_link.