Instantiate Custom Metadata Types in Apex

You can now edit custom metadata records in memory within Apex. Previously, custom metadata records queried with SOQL in Apex were immutable. Audit fields (CreatedDate, CreatedBy, LastModifiedDate, LastModifiedBy, SystemModStamp) and calculated fields remain uneditable. DML operations aren’t allowed on custom metadata in Apex or the Partner or Enterprise APIs. DML operations can be done with the Apex Metadata API.

Where: This change applies to Professional, Enterprise, Performance, Unlimited, Developer, and Database.com editions. Professional Edition orgs can create, edit, and delete custom metadata records only in installed package types.

How: In this example, the first method is instantiating a custom metadata record, but no records are inserted into memory. The second method retrieves a record, changes it, and returns it to the caller, but the change is not updated in the database.
public class CustomMetadataService {

    public CustomMetadataService() {}

    /**
     * This method instantiates a custom metadata record of type MyCustomMetadataType__mdt
     * and sets the DeveloperName to the input String.
     * Note that the record is not inserted into the database, 
     * and would not be found by a SOQL query.
     */
    public MyCustomMetadataType__mdt getCustomMetadataRecord(String myName) {
        MyCustomMetadataType__mdt theRecord = new MyCustomMetadataType__mdt();
        theRecord.DeveloperName = myName;
        return theRecord;
    }

    /**
     * This method retrieves a custom metadata record, changes a field, and returns it
     * to the caller. Note that the changed record is not updated in the database.
     */
    public MyCustomMetadataType__mdt getChangedCustomMetadataRecord(String myNewName) {
        MyCustomMetadataType__mdt theRecord = [SELECT Id, DeveloperName from MyCustomMetadataType__mdt LIMIT 1];
        theRecord.DeveloperName = myNewName;
        return theRecord;
    }

}