Open iTunes. Create a smart group that includes music that is not on your computer (location = iCloud). Make sure the group has live updating. Create a second group that includes music where iCloud status is "matched", file location is on your computer, and quality is under 256 Kbps. Again, ensure live updating.
In the second group, select all and delete the tracks - but do NOT delete from iCloud, only locally (you'll get a pop-up - it will ask if you also want to delete from iCloud - DO NOT choose yes!!). This should clear all low-quality local files for anything in Apple Music.
Go back to the first list. Select all and click the cloud icon to download the files locally. Keep doing this until this list is empty (that is, there are no tracks left that are only saved in iCloud).
Go to your Music folder. Everything you have in your iTunes library will be here (~/Music/iTunes, probably in a subdirectory such as "iTunes Media"). Copy everything here to an external drive. That should do it.
Note that you can ignore any files in the iTunes Media folder that are not music, such as iOS apps. Also note that some media will not transfer to another platform (iTunes album extras, some video content). You may also want to check for DRM on any older tracks - you can remove DRM by selecting the file and choosing "Create AAC Version" to make a copy with no rights management, or (for matched files) deleting the local copy and re-downloading it from iCloud.