Why are portrait photos sometimes sideways? When I take a portrait photo, it shows up correctly on the phone.  But when I transfer it to certain software, it shows up flipped sideways.  Why is this?  Is it the fault of the phone or the software that's reading the image?  Is there a way to fix a batch of these images so that all software shows them correctly?

The phone is an LG G3, but it's happened to me on multiple phones, and many people have reported this problem, e.g.:
https://androidforums.com/threads/vertical-photos-rotating-to-horizontal-orientation.588960/

The same thing happens if I use a different camera app, OpenCamera, instead of the stock Android app.
I found the answer to my own question.   It seems cameras take the picture and then store an flag, the Exif orientation, rather than just storing the rotated data, because the latter would be too time-consuming or it would be too expensive to give the camera custom hardware. (https://www.impulseadventure.com/photo/exif-orientation.html)

But not all software supports these image rotation flags, so it's best to process images when importing them to convert them to an image with no rotation flag.  Some tools for doing this are here:

http://how-to.wikia.com/wiki/How_to_auto-rotate_digital_photos_to_their_proper_orientation

The exifautotran script seems to work for me, e.g.:

exifautotran *.jpg